我有一条select语句,我想从表中选择1条记录。结构为:
id | start_time
--------------
1 NULL
2 2014-08-23
3 2014-09-01
我想选择开始时间为NULL的项目,但是如果不存在该项目,则希望选择最新的项目start_time
。我尝试使用ORDER
with LIMIT 1
,但是使用ORDER BY start_time
要么先给出NULL,然后是最早的开始,或者先给出NULL,然后再给出NULL。是否有结果顺序1,3,2
?
您可以使用两个排序表达式来获取所需的顺序:
select t.*
from table t
order by (start_time is null) desc,
start_time desc
limit 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句