sql 排序时空值排最后面
sql 排序问题:
1.假设排序时我们用时间最为排序条件,那么在倒序时会出现空值排在前面,有值的排在后面。
解决:将排序字段加 is null;
例1:空值排在前面
select * from sys_user order by update_date desc;
如图:
2.例2:空值排在后面,其他正常排序。(加 is null)
select * from sys_user order by update_date is null, update_date desc;
如图:
第二中方法:select * from sys_user ORDER BY update_date NULLS LAST
;
NULLS FIRST
和NULLS LAST
选项可以决定在排序操作中在 non-null 值之前还是之后。
默认情况下,空值大于任何非空值;也就是说,DESC
排序默认是NULLS FIRST
,否则为NULLS LAST
。