null排在有值的行前面还是后面通过语法来指定
--null值在前
select * from tablename order by id nulls first;
--null值在后
select * from tablename order by id nulls last;
--null在前配合desc使用
select * from tablename order by id desc nulls first;
--null在后配合desc使用
select * from tablename order by id desc nulls last;
举例:
null值在后,先按照count1降序排列,count1相同再按照count2降序排列
order by count1 desc nulls last, count2 desc nulls last;
要是还有不太明白的地方请留言,评论必回
要是对我的文章感兴趣的话,关注一下吧,谢谢!
本文详细介绍了在SQL中如何通过语法指定NULL值在查询结果中的位置,包括将NULL值排在有值的行前面或后面的技巧。同时,还提供了如何在多列排序中结合DESC关键字使用NULL FIRST和NULL LAST的示例。
9963





