Page 1 of 1

mysql UNION question

Posted: Sat Oct 13, 2007 3:32 pm
by JustinMs66
my query:

Code: Select all

SELECT * FROM `table1` UNION ALL SELECT * FROM `table2` ORDER BY `date` desc
#1) when i am getting the data using mysql_fetch_array, how can i tell which table the row is from?

#2) how can i get everything to order by `date` desc? like right now, that does order it by date desc, except it does the 1st table 1st, the 2nd table 2nd.

Choose the Right Board

Posted: Sat Oct 13, 2007 5:04 pm
by s.dot
I don't think you need the ALL after union

You can't tell which table it's from, if they have the same field names. If they have different field names, then it will be easy to tell which table it's from. If they have the same field names, you can use column aliases to bypass that.

To order by date, put your (SELECT ...) in parenthesis (both of them), but not the union keyword.

Moved to databases.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.