如下表t_user:
[table]
|name|age
|zhangsan|1
|lisi|NULL
|wangwu|2
[/table]
执行一下sql:
[table]
|name|age
|lisi|NULL
|zhangsan|1
|wangwu|2
[/table]
实际上我们是想将没有填写age的记录放在最后,我们可以
[table]
|name|age
|zhangsan|1
|wangwu|2
|lisi|NULL
[/table]
为什么会这样?可以这样来理解:
等价于:
看出来了吗?
[table]
|name|age
|zhangsan|1
|lisi|NULL
|wangwu|2
[/table]
执行一下sql:
select * from t_user order by age;[table]
|name|age
|lisi|NULL
|zhangsan|1
|wangwu|2
[/table]
实际上我们是想将没有填写age的记录放在最后,我们可以
select * from t_user order by age is null, age;[table]
|name|age
|zhangsan|1
|wangwu|2
|lisi|NULL
[/table]
为什么会这样?可以这样来理解:
select * from t_user order by age is null, age;等价于:
select * from (select name, age, (age is null) as isnull from t_user) as foo order by isnull, age;看出来了吗?
本文介绍了一种在SQL中处理带有NULL值字段的排序方法,通过使用age is null, age的排序方式,可以有效地将NULL值记录放置在结果集的末尾。
1609

被折叠的 条评论
为什么被折叠?



