MySQL联合查询,按2个变量排序

时间:2014-09-21 23:51:50

标签: php mysql sql

每个表格(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

//// EDIT

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

////编辑

2 个答案:

答案 0 :(得分:3)

您真正需要做的是更仔细地考虑您的架构。考虑将日期时间列命名为相同,然后运行这样的查询 - http://sqlfiddle.com/#!2/a3b4c/7/0

SELECT selection, id, datetimefoo, user FROM (
  SELECT 
    1 AS selection, 
    table1.id, table1.datetimefoo, 
    table3.user 
    FROM table1 
    LEFT OUTER JOIN table2 
    ON table1.id = table2.table1id
    LEFT OUTER JOIN table3
    ON table1.id = table3.id


  UNION

  SELECT 
    2 AS selection, 
    table1.id, table1.datetimefoo, 
    table3.user 
    FROM table1 
    INNER JOIN table2 
    ON table1.id = table2.table1id
    INNER JOIN table3
    ON table1.id = table3.id

  ) AS T2
ORDER BY datetimefoo DESC

在SQL小提琴中,这会产生更接近您正在寻找的结果。我仍然不确定为什么你在第二个问题上需要INNER JOINS - 你没有在这里做任何需要它们的事情。

以下是另一种不需要更改列名的方法,但需要可排序列的别名 - http://sqlfiddle.com/#!2/ec4bc/3/0

SELECT * FROM (
  SELECT 
    1 AS selection, 
    table1.id, table1.datetimefoo AS sort_date, -- alias on first table's date
    table2.datetimebar,
    table3.user 
    FROM table1 
    LEFT OUTER JOIN table2 
    ON table1.id = table2.table1id
    LEFT OUTER JOIN table3
    ON table1.id = table3.id


  UNION

  SELECT 
    2 AS selection, 
    table1.id, table1.datetimefoo,
    table2.datetimebar AS sort_date, -- alias on second table's date
    table3.user 
    FROM table1 
    INNER JOIN table2 
    ON table1.id = table2.table1id
    INNER JOIN table3
    ON table1.id = table3.id

  ) AS T2
ORDER BY sort_date DESC

答案 1 :(得分:2)

我相信你的任务过于复杂:

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