MySQL UNION,从2个表中按日期时间排序

时间:2014-09-22 15:17:57

标签: mysql sql

//// EDIT

对于那些可能会遇到长而复杂的MySQL请求的人,请在PhpmyAdmin中尝试一下!它会告诉你错误!

////编辑

每个表格(table1& table2)都有自己的DATETIME字段 我正在尝试捕获两个表的id并按DATETIME字段对它们进行排序。

示例:

Table 1                        Table 2
------------                 -------------
id |  datetime1               id | table1id  | datetime2
------------------------      -----------------------
1  |  2014-09-21 20:31:26     1  | 2         | 2014-09-21 20:31:29
2  |  2014-09-21 20:31:27     2  | 3         | 2014-09-21 20:31:30
3  |  2014-09-21 20:31:28

Table 3                      
------------               
id |  user               
------------------------   
2  |  phil                
3  |  nathalie

此尝试未正确输出我的输出:

SELECT *
FROM (
    SELECT 
    1 AS selection, 
    table1.id, table1.datetime1, 
    table2.datetime2 
    table3.user 
    FROM Table1 
    LEFT OUTER JOIN table2 
    ON table1.id = table2.table1id
    LEFT OUTER JOIN table3
    ON table1.id = table3.id

    UNION ALL

    SELECT 
    2 AS selection, 
    table1.id, table1.datetime1, 
    table2.datetime2 
    table3.user 
    FROM Table1 
    INNER JOIN table2 
    ON table1.id = table2.table1id
    INNER JOIN table3
    ON table1.id = table3.id
) AS query
ORDER BY table1.datetime1 DESC, table2.datetime2 DESC 

所需数据:
来自表2 id:2,1,
从表1 id:3,2,1 所以:2,1,3,2,1

2 个答案:

答案 0 :(得分:0)

SELECT 
1 AS selection, 
table1.id, table1.datetime1, 
table2.datetime2 
table3.user 
FROM Table1 
LEFT OUTER JOIN table2 
ON table1.id = table2.table1id
LEFT OUTER JOIN table3
ON table1.id = table3.id
ORDER BY table2.datetime2 DESC 

UNION ALL

SELECT 
2 AS selection, 
table1.id, table1.datetime1, 
table2.datetime2 
table3.user 
FROM Table1 
INNER JOIN table2 
ON table1.id = table2.table1id
INNER JOIN table3
ON table1.id = table3.id
ORDER BY table1.datetime1 DESC

ORDER BY需要在每个查询上只有一个UNION。

答案 1 :(得分:0)

您需要为每个子查询提供自己的order by子句:

SELECT *
 FROM (
    SELECT 
    1 AS selection, 
    table1.id, table1.datetime1, 
    table2.datetime2 
    table3.user 
    FROM Table1 
    LEFT OUTER JOIN table2 
    ON table1.id = table2.table1id
    LEFT OUTER JOIN table3
    ON table1.id = table3.id
    ORDER BY table1.datetime1 DESC
   )t1

  UNION ALL SELECT *
    FROM (
    SELECT 
    2 AS selection, 
    table1.id, table1.datetime1, 
    table2.datetime2 
    table3.user 
    FROM Table1 
    INNER JOIN table2 
    ON table1.id = table2.table1id
    INNER JOIN table3
    ON table1.id = table3.id
    ORDER BY table2.datetime2 DESC
    ) t2

修改 所以在这种情况下,您应该将所有日期时间放在一个列中,并在外部子句

中排序
        SELECT *
 FROM (
    SELECT 
    1 AS selection, 
    table1.id, table1.datetime1 as dtime, 
    table3.user 
    FROM Table1 
    LEFT OUTER JOIN table2 
    ON table1.id = table2.table1id
    LEFT OUTER JOIN table3
    ON table1.id = table3.id
   )t1

  UNION ALL SELECT *
    FROM (
    SELECT 
    2 AS selection, 
    table1.id,
    table2.datetime2 
    table3.user 
    FROM Table1 
    INNER JOIN table2 
    ON table1.id = table2.table1id
    INNER JOIN table3
    ON table1.id = table3.id
    ) t2

    ORDER BY dtime DESC