0

假设我们有桌子

身份证、头衔、日期

我需要建立1个查询:

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

1 回答 1

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示例。

祝你好运。

于 2013-01-31T06:16:38.780 回答