升序排列,默认情况下,null值排后面。
降序排序,默认情况下,null值排前面。
有几种办法改变这种情况:
(1)用 nvl 函数或decode 函数 将null转换为一特定值
如:
select m.nechen, m.lastlogtime
from memberinfo m
where m.PERSONALSSTATE = '征友进行中'
and (to_number(to_char(m.BIRTHDAY, 'yyyy')) between 1974 and 1983)
and (m.PHOTOPATH is not null)
and m.SEX = '女'
order by decode(m.LASTLOGTIME,null,to_date('2008-3-18','yyyy-MM-dd'),m.LASTLOGTIME) desc
(2)用case语法将null转换为一特定值,但使用中发现,对日期型null识别不了。(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then '北京漂客' else mycol end)
(3)使用nulls first 或者nulls last 语法。 (但hibernate不支持)
nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first
null last :将null排在最后面。如:select * from mytb order by mycol nulls last
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 desc
如果要想让含有null的列按照自己的意愿进行排序,可做如下处理。
降序排序,默认情况下,null值排前面。
有几种办法改变这种情况:
(1)用 nvl 函数或decode 函数 将null转换为一特定值
如:
select m.nechen, m.lastlogtime
from memberinfo m
where m.PERSONALSSTATE = '征友进行中'
and (to_number(to_char(m.BIRTHDAY, 'yyyy')) between 1974 and 1983)
and (m.PHOTOPATH is not null)
and m.SEX = '女'
order by decode(m.LASTLOGTIME,null,to_date('2008-3-18','yyyy-MM-dd'),m.LASTLOGTIME) desc
(2)用case语法将null转换为一特定值,但使用中发现,对日期型null识别不了。(oracle9i以后版本支持。和sqlserver类似):
order by (case mycol when null then '北京漂客' else mycol end)
(3)使用nulls first 或者nulls last 语法。 (但hibernate不支持)
nulls first :将null排在最前面。如:select * from mytb order by mycol nulls first
null last :将null排在最后面。如:select * from mytb order by mycol nulls last
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 desc
如果要想让含有null的列按照自己的意愿进行排序,可做如下处理。