mysql中null值的排序问题分析
 
如下表t_user: 
nameage
zhangsan1
lisiNULL
wangwu2
  www.2cto.com  
执行一下sql: 
Sql代码  
select * from t_user order by age;  
 
nameage
lisiNULL
zhangsan1
wangwu2
 
实际上我们是想将没有填写age的记录放在最后,我们可以 
Sql代码  
select * from t_user order by age is null, age;  
 
nameage
zhangsan1
wangwu2
lisiNULL
 
为什么会这样?可以这样来理解: 
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;  
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
猜你喜欢
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
  • 2021-04-15
  • 2022-02-26
  • 2021-09-13
  • 2021-08-08
相关资源
相似解决方案