sql非空白字符串、空白字符串和null值排序
1、mysql
ASC(升序):依次为 null值、空白字符串、非空白字符串
DESC(降序):依次为非空白字符串、空白字符串、null值
2、postgres
ASC(升序):依次为 空白字符串、非空白字符串、null值
DESC(降序):依次为null值、非空白字符串、空白字符串
但有时需要见null值最后显示,显然postgres默认排序是不行的,我们需要通过语法来指定。
-- desc null值在后
select * from test_tb order by name desc nulls last,id desc;
-- desc null值在前
select * from test_tb order by name desc nulls first,id desc;
-- 或
select * from test_tb order by name desc,id desc;
-- asc同理