MYSQL 排序时, 字段为空的排在最后面
假设场景
表名
表名: user_information
表名描述: 用户信息表
包含字段:id, name, age, contact_relation(自定义的一个字典的code, 意义为用户类别)
按用户类别取数,排序为倒序
使用order by contact_relation desc
实现降序时,contact_relation 为 null 数据的会排在数据的最后面;
SQL:
SELECT id, contact_relation FROM `user_information`
order by `contact_relation` desc;
按用户类别取数,排序为正序
但是,order by contact_relation asc 升序时,contact_relation 为null的数据则会排在最前面.
SQL:
SELECT id, contact_relation FROM `user_information`
order by `contact_relation` asc;
解决正序时,字段为空的排在最后面
如果想要将 contact_relation 为null的数据排在最后,就需要加上 is null。
SQL:
SELECT id, contact_relation FROM `user_information`
order by `contact_relation` is null, `contact_relation` asc;
~ end
正文到此结束
热门推荐
广告是为了更好的提供数据服务