Mysql自我加入多个订单

时间:2013-01-31 06:11:51

标签: mysql join

假设我们有表

id,title,date

我需要构建1个查询:

select date = TODAY, order by id
select data < TODAY, order by date desc,
select data > TODAY, order by date asc,

1 个答案:

答案 0 :(得分:2)

我认为你需要使用UNION和子查询:

SELECT * FROM (
    SELECT *
    FROM YourTable
    WHERE Date(dateField) = Date(Now())
    ORDER BY ID 
) t1
UNION
SELECT * FROM (
    SELECT *
    FROM YourTable
    WHERE dateField < Now()
    ORDER BY dateField DESC
) t2
UNION
SELECT * FROM (
    SELECT *
    FROM YourTable
    WHERE Date(dateField) > Now()
    ORDER BY dateField 
) t3

这是一个简化的SQL Fiddle示例。

祝你好运。