AJ OP
AJ OP

Reputation: 51

How to SELECT fields from 2 table order by datetime-field from both table?

my table info: table 'entry1' have fields 'id' 'title' 'username' 'date_post' and other fields table 'entry2' have fields 'id' 'title' 'username' 'date_post' ant other fields

with more info: 1) 'id' in table 'entry1' is not the same as 'id' in table 'entry2' 2) field 'date_post' from both table are type of datetime

what i want: I want to select 'id' 'title' 'username' and 'date_post' from both table and order by 'date_post' DESC from both table in a statement.

Sorry all, I'm very new to PHP and my english is not good. It's very difficult to ask.

Upvotes: 0

Views: 603

Answers (2)

AJ OP
AJ OP

Reputation: 51

My working code is:

$sql="(SELECT title, date_post FROM entry1 WHERE user_id=$user_id) UNION 
      (SELECT title, date_post FROM entry2 WHERE user_id=$user_id) UNION
      (SELECT title, date_post FROM entry3  WHERE user_id=$user_id)
      ORDER  BY date_post DESC";
$result=mysql_query($sql);

Upvotes: 1

Jonas T
Jonas T

Reputation: 3077

Assume that both table have same identical column names and types. Try this.

SELECT * FROM entry1
UNION
SELECT * FROM entry2
ORDER BY date_post DESC

Upvotes: 0

Related Questions