Mysql 排序null值 排序问题分析
2015-07-30 14:44
627 查看
mysql中null值的排序问题分析
如下表t_user:
name age
zhangsan 1
lisi NULL
wangwu 2
www.2cto.com
执行一下sql:
Sql代码
select * from t_user order by age;
name age
lisi NULL
zhangsan 1
wangwu 2
实际上我们是想将没有填写age的记录放在最后,我们可以
Sql代码
select * from t_user order by age is null, age;
name age
zhangsan 1
wangwu 2
lisi NULL
为什么会这样?可以这样来理解:
Sql代码
select * from t_user order by age is null, age;
等价于:
Sql代码
select * from (select name, age, (age is null) as isnull from t_user) as foo order by isnull, age;
如下表t_user:
name age
zhangsan 1
lisi NULL
wangwu 2
www.2cto.com
执行一下sql:
Sql代码
select * from t_user order by age;
name age
lisi NULL
zhangsan 1
wangwu 2
实际上我们是想将没有填写age的记录放在最后,我们可以
Sql代码
select * from t_user order by age is null, age;
name age
zhangsan 1
wangwu 2
lisi NULL
为什么会这样?可以这样来理解:
Sql代码
select * from t_user order by age is null, age;
等价于:
Sql代码
select * from (select name, age, (age is null) as isnull from t_user) as foo order by isnull, age;
相关文章推荐
- Mysql之入门命令
- Navicat For Mysql快捷键
- 【未测试】采用Atlas+Keepalived实现MySQL读写分离、读负载均衡
- 【对比mysql各种代理】mysql中间件研究(Atlas奇虎360,cobar阿里巴巴,TDDL淘宝分布式数据层)
- mysql TIMESTAMP(时间戳)详解
- 【未测试】使用mysql-proxy 快速实现mysql 集群 读写分离
- mysql导入数据错误
- MySQL函数
- [MySQL优化案例]系列 — 分页优化
- 未测试---- mysql+spring+mybatis实现数据库读写分离[代码配置]
- mysql分组后取前几条数据
- mysql中SELECT INTO 和 replace into SELECT 两种表复制语句
- mysql中SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
- mysql配置文件my.cnf解析
- 小试mysql分布式
- Ubuntu 下安装mysql数据库
- Mysql 存储过程查询结果赋值到变量的方法
- MySQL Sharding详解
- MySQL常见查询语句
- mysql报Fatal error encountered during command execution的解决办法