Home > Blockchain >  SQL Query to order 2 different tables with a similar attribute
SQL Query to order 2 different tables with a similar attribute

Time:06-15

I've 2 tables, they both have a structure similar to this one

id | content | date

However I would like to a make a query where the 2 tables are mixed together and ordered by their date

for example in the TABLE_A I've (1,Joe,20-11-2020)/(2,John,20-11-2021) TABLE_B has (1,Luke,20-11-2010)/(1,Mark,20-11-2011) The result I want to see is: (1,Luke,20-11-2010)/(1,Mark,20-11-2011)/(1,Joe,20-11-2020)/(2,John,20-11-2021)

CodePudding user response:

 SELECT T.ID,T.CONTENT,T.DATE
 FROM TABLE_1 AS T
   UNION ALL
 SELECT A.ID,A.CONTENT,A.DATE
 FROM TABLE_2 AS A
 ORDER BY 3

CodePudding user response:

try this:

select id, content, date
from (
select id, content, date
from tab_A
union all
select id, content, date
from tab_B
) C order by date

use "union" instead of "union all" if you are not interest to have duplicates rows

  • Related