1.不加“关照”的order by升序排序效果--NULL值在后。
sec@ora10g> select * from t order by x;
X
----------
1
2
3
4
7 rows selected.
2.不加“关照”的order by降序排序效果--NULL值在前。
sec@ora10g> select * from t order by x desc;
X
----------
4
3
2
1
7 rows selected.
3.特殊“关照”的order by升序排序效果--NULL值在前。
sec@ora10g> select * from t order by x nulls first;
X
----------
1
2
3
4
7 rows selected.
4.特殊“关照”的order by降序排序效果--NULL值在后。
sec@ora10g> select * from t order by x desc nulls last;
X
----------
4
3
2
1
7 rows selected.
5.规律总结
1)不加“关照”的情况下,我们可以把那些NULL值假想为所有内容中值是最大的,因此,升序排序后NULL值在最后,倒序排序后NULL值在最前!
2)特殊“关照”的情况下,当指定“NULLS FIRST”时,无论是升序排序还是倒序排序,NULL值都会排列在最前面;当指定“NULLS LAST”时,无论是升序排序还是倒序排序,NULL值都会排列在最后面。
6.Oracle官方文档中有关“NULLS FIRST | NULLS LAST”的参考内容
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2171079
摘录在此:
NULLS FIRST | NULLS LAST
Specify whether returned rows containing null values should appear first or last in the ordering sequence.
NULLS LAST is the default for ascending order, and NULLS FIRST is the default for descending order.
7.小结
通过这篇内容的介绍,我们可以看到Oracle的确是无微不至。有了这个NULL值位置的强制方法,我们在书写SQL时的灵活性和可控性便可进一步得到加强
NULLS FIRST: place null value first排序时对空值的位置排列
最新推荐文章于 2023-11-14 16:30:49 发布