mysql UNION question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

mysql UNION question

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Choose the Right Board

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply