我试图通过最新更新日期时间来查询查询,该日期时间大于现在上升然后小于现在下降。这是我想要的输出:
2014-06-01 00:00:00
2014-06-04 00:00:00
2014-06-05 00:00:00
2014-06-06 00:00:00
2014-05-19 00:00:00
2014-05-15 00:00:00
0000-00-00 00:00:00
但是我得到了这个:
2014-06-01 00:00:00
2014-06-04 00:00:00
2014-06-05 00:00:00
2014-06-06 00:00:00
0000-00-00 00:00:00
2014-05-15 00:00:00
2014-05-19 00:00:00
我使用的查询是:
SELECT lastupdated FROM users ORDER BY lastupdated > NOW() DESC, lastupdated ASC;
答案 0 :(得分:2)
您可以使用UNION
:
SELECT lastupdated
FROM users
WHERE lastupdated > NOW()
ORDER BY lastupdated DESC
UNION
SELECT lastupdated
FROM users
WHERE lastupdated <= NOW()
ORDER BY lastupdated ASC;
答案 1 :(得分:1)
有什么问题?日期0000-00-00
已过去,因此该列表首先列出。如果你想要它总体上,请明确地把它放在那里:
order by (case when lastupdated = '0000-00-00 00:00:00' then 1
when lastupdate > now() then 2
else 3
end),
lastupdated asc;
如果您希望最后一组的排序降序,请执行:
order by (lastupdate > now()) desc,
(case when lastupdate > now() then lastupdate end) asc,
lastupdate desc
答案 2 :(得分:-1)
明确地对时间戳进行排序&lt; NOW()类似于您的排序方式&gt; NOW()
SELECT lastupdated FROM users ORDER BY lastupdated > NOW() DESC, lastupdated <= NOW() ASC;