Home >Database >Mysql Tutorial >mysql中null值的排序问题分析_MySQL

mysql中null值的排序问题分析_MySQL

WBOY
WBOYOriginal
2016-06-01 13:36:061198browse

bitsCN.com

mysql中null值的排序问题分析

 

如下表t_user: 

name age

zhangsan 1

lisi NULL

wangwu 2

执行一下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;  

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn