【sqlserver】:
sqlserver 認(rèn)為 null 最小。
升序排列:null 值默認(rèn)排在最前。
要想排后面,則:order by case when col is null then 1 else 0 end ,col
降序排列:null 值默認(rèn)排在最后。
要想排在前面,則:order by case when col is null then 0 else 1 end , col desc
【oracle】:
oracle認(rèn)為 null 最大。
升序排列,默認(rèn)情況下,null值排后面。
降序排序,默認(rèn)情況下,null值排前面。
有幾種辦法改變這種情況:
(1)用 nvl 函數(shù)或decode 函數(shù) 將null轉(zhuǎn)換為一特定值
(2)用case語(yǔ)法將null轉(zhuǎn)換為一特定值(oracle9i以后版本支持。和sqlserver類(lèi)似): order by (case mycol when null then ’北京漂客’ else mycol end)
(3)使用nulls first 或者nulls last 語(yǔ)法。
這是oracle專(zhuān)門(mén)用來(lái)null值排序的語(yǔ)法。
nulls first :將null排在最前面。如:select * from mytb order by mycol nulls first
null last :將null排在最后面。如:select * from mytb order by mycol nulls last