StackPhp
StackPhp

Reputation: 1

Join two tables according to time DESC

I have two tables with different column names and datetime.

I want to display data from both the tables according to time descending.

Table 1 index,music_added,time

Table 2 index,photo_added,time

Right Now I display them in two seperate queries like "select * from table1 ORDER BY time DESC" and "select * from table2 ORDER BY time DESC".

But I want the resultset to be mixture of both the tables 1 and 2 sorted to time DESC. So how should I use JOIN or any other query to merge the rows according to time?

Upvotes: 0

Views: 47

Answers (1)

eggyal
eggyal

Reputation: 125835

Use UNION:

  SELECT index,music_added,time,'music' AS type
UNION ALL
  SELECT index,photo_added,time,'photo' AS type
ORDER BY time DESC

Upvotes: 4

Related Questions