MySQL union query, order by 2 variables

前端 未结 2 2059
醉酒成梦
醉酒成梦 2021-01-22 10:13

Each table (table1 & table2) has its own DATETIME field.
I\'m trying to catch id\'s of the two tables and order them by their DATETIME field.

Example:

2条回答
  •  抹茶落季
    2021-01-22 10:41

    I believe you are over-complicating a rather straight-forward task:

    SELECT *
    FROM (
        SELECT 1 AS selection, table1.id as id, table1.datetime1 as date FROM Table1 
        UNION ALL
        SELECT 2 AS selection, table2.id as id, table2.datetime2 as date FROM Table2 
    ) AS query
    ORDER BY date DESC 
    

提交回复
热议问题