关于数据库中字段空值的SQL排序的问题
2017-08-17 22:35
225 查看
在Oracle中进行查询排序时,如果排序字段里面有空值的情况下,排序结果可能会达不到自己想要的结果。
如 select * from tableTest order by VISITS desc
将原来的sql语句改写为:
select * from tableTest
order by VISITS desc nulls last
"nulls last"控制将空值记录放在后面,当然,你也可以用"nulls first"将控制记录放在前面。
1、oracle 空值处理,排序过滤
oracle认为 null 最大。
升序排列,默认情况下,null值排后面。
降序排序,默认情况下,null值排前面。
有几种办法改变这种情况:
(1)用 nvl 函数或decode 函数 将null转换为一特定值
(2)用case语法将null转换为一特定值(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then ’0’ else mycol end)
(3)使用nulls first 或者nulls last 语法。
这是oracle专门用来null值排序的语法。
nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first
nulls last :将null排在最后面。如:select * from mytb order by mycol nulls last
2、sqlserver:
sqlserver 认为 null 最小。
升序排列:null 值默认排在最前。
要想排后面,则:order by case when col is null then 1 else 0 end ,col
降序排列:null 值默认排在最后。
要想排在前面,则:order by case when col is null then 0 else 1 end , col d
在Oracle中进行查询排序时,如果排序字段里面有空值的情况下,排序结果可能会达不到自己想要的结果。
如 select * from tableTest order by VISITS desc
将原来的sql语句改写为:
select * from tableTest
order by VISITS desc nulls last
"nulls last"控制将空值记录放在后面,当然,你也可以用"nulls first"将控制记录放在前面。
1、oracle 空值处理,排序过滤
oracle认为 null 最大。
升序排列,默认情况下,null值排后面。
降序排序,默认情况下,null值排前面。
有几种办法改变这种情况:
(1)用 nvl 函数或decode 函数 将null转换为一特定值
(2)用case语法将null转换为一特定值(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then ’0’ else mycol end)
(3)使用nulls first 或者nulls last 语法。
这是oracle专门用来null值排序的语法。
nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first
nulls last :将null排在最后面。如:select * from mytb order by mycol nulls last
2、sqlserver:
sqlserver 认为 null 最小。
升序排列:null 值默认排在最前。
要想排后面,则:order by case when col is null then 1 else 0 end ,col
降序排列:null 值默认排在最后。
要想排在前面,则:order by case when col is null then 0 else 1 end , col d
如 select * from tableTest order by VISITS desc
将原来的sql语句改写为:
select * from tableTest
order by VISITS desc nulls last
"nulls last"控制将空值记录放在后面,当然,你也可以用"nulls first"将控制记录放在前面。
1、oracle 空值处理,排序过滤
oracle认为 null 最大。
升序排列,默认情况下,null值排后面。
降序排序,默认情况下,null值排前面。
有几种办法改变这种情况:
(1)用 nvl 函数或decode 函数 将null转换为一特定值
(2)用case语法将null转换为一特定值(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then ’0’ else mycol end)
(3)使用nulls first 或者nulls last 语法。
这是oracle专门用来null值排序的语法。
nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first
nulls last :将null排在最后面。如:select * from mytb order by mycol nulls last
2、sqlserver:
sqlserver 认为 null 最小。
升序排列:null 值默认排在最前。
要想排后面,则:order by case when col is null then 1 else 0 end ,col
降序排列:null 值默认排在最后。
要想排在前面,则:order by case when col is null then 0 else 1 end , col d
在Oracle中进行查询排序时,如果排序字段里面有空值的情况下,排序结果可能会达不到自己想要的结果。
如 select * from tableTest order by VISITS desc
将原来的sql语句改写为:
select * from tableTest
order by VISITS desc nulls last
"nulls last"控制将空值记录放在后面,当然,你也可以用"nulls first"将控制记录放在前面。
1、oracle 空值处理,排序过滤
oracle认为 null 最大。
升序排列,默认情况下,null值排后面。
降序排序,默认情况下,null值排前面。
有几种办法改变这种情况:
(1)用 nvl 函数或decode 函数 将null转换为一特定值
(2)用case语法将null转换为一特定值(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then ’0’ else mycol end)
(3)使用nulls first 或者nulls last 语法。
这是oracle专门用来null值排序的语法。
nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first
nulls last :将null排在最后面。如:select * from mytb order by mycol nulls last
2、sqlserver:
sqlserver 认为 null 最小。
升序排列:null 值默认排在最前。
要想排后面,则:order by case when col is null then 1 else 0 end ,col
降序排列:null 值默认排在最后。
要想排在前面,则:order by case when col is null then 0 else 1 end , col d
相关文章推荐
- 关于数据库中字段空值的SQL排序的问题
- 关于Oracle数据库中SQL空值排序的问题
- 关于Oracle数据库中SQL空值排序的问题
- 关于Oracle数据库中SQL空值排序的问题
- 关于oracle 数据库中字符字段的排序问题
- 关于Oracle数据库中SQL空值排序的问题
- 关于SQL查询的字符串类型字段的排序问题
- 一个关于时间的SQL分组排序问题
- 关于数据库Varchar字段类型长度设计问题
- 关于在数据库字段中添加换行符的问题
- 关于数据库中字段为日期的模糊查询问题
- sql关于数据库远程无法登陆的问题
- SQL order by ID desc/asc加一个排序的字段解决查询慢问题
- 关于删除数据库中含有自增长字段的表,清空后,自增长字段重新从1开始的问题解决方法。
- 关于数据库Varchar字段类型长度设计问题(转载)
- 关于数据库Varchar字段类型长度设计问题
- 关于sql语句group by 排序问题~
- 数据库时间字段排序问题
- 关于数据库空字段和DEFAULT值等问题
- 关于数据库空值和DataSet.GetXML()函数的问题