0

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.

5
Contributors
4
Replies
23
Views
4 Years
Discussion Span
Last Post by diafol
1

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
0

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

0

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

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.