Hello,
I'm tring to develop a mysql news scroller for my website.
The problem that i'm facing is that i need to have multiple mysql querys joing together beceuase i have multple categories like:

select * from news where category LIKE '$cat1' order by id desc limit 1
select * from news where category LIKE '$cat2' order by id desc limit 1
select * from news where category LIKE '$cat3' order by id desc limit 1

I don't know how to join then to work fast and lean for the server.

Recommended Answers

All 4 Replies

You can create a UNION. It's a little weird but should work:

select * from (select * from news where category LIKE '$cat1' order by id desc limit 1) a
union
select * from (select * from news where category LIKE '$cat2' order by id desc limit 1) b
union
select * from (select * from news where category LIKE '$cat3' order by id desc limit 1) c

pritaeas was right. but it depends on your own expected output, Union is one, but other than Union, there was Join, Right Join, Left Join and outer Join.

Union by it words are combining 2 or more tables by multiple queries, Join is what are the common values between 2 or more tables, Left join what is Common to the first table with the second table, in vice versae of the left Join was the right Join, and the Outer Join is the vice versae of Join

You can also consider a sub-query. You can find a guiding tutorial here

Member Avatar for diafol

The UNION approach would be my choice in this example. Why was webville downvoted??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.