Oralce对NULL值的排序后的位置有一个特殊的“关照”,这就是“NULLS FIRST”和“NULLS LAST”选项,使用这个选项便可以在SQL排序中强制指定NULL值出现的位置(是显示在最前,还是显示在最后)。演示并总结在此,供参考。1.创建演示表T并初始化7条数据sec@ora10g> create table t (x int);sec@ora10g> insert into t values (1);sec@ora10g> insert into t values (2);sec@ora10g> insert into t values (3);sec@ora10g> insert into t values (4);sec@ora10g> insert into t values (null);sec@ora10g> insert into t values (null);sec@ora10g> insert into t values (null);sec@ora10g> commit;2.不加“关照”的order by升序排序效果--NULL值在后。sec@ora10g> select * from t order by x;X----------12347 rows selected.3.不加“关照”的order by降序排序效果--NULL值在前。sec@ora10g> select * from t order by x desc;X----------43217 rows selected.4.特殊“关照”的order by升序排序效果--NULL值在前。sec@ora10g> select * from t order by x nulls first;X----------12347 rows selected.5.特殊“关照”的order by降序排序效果--NULL值在后。sec@ora10g> select * from t order by x desc nulls last;X----------43217 rows selected.6.规律总结1)不加“关照”的情况下,我们可以把那些NULL值假想为所有内容中值是最大的,因此,升序排序后NULL值在最后,倒序排序后NULL值在最前!2)特殊“关照”的情况下,当指定“NULLS FIRST”时,无论是升序排序还是倒序排序,NULL值都会排列在最前面;当指定“NULLS LAST”时,无论是升序排序还是倒序排序,NULL值都会排列在最后面。7.Oracle官方文档中有关“NULLS FIRST | NULLS LAST”的参考内容摘录在此:NULLS FIRST | NULLS LASTSpecify 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.8.小结通过这篇内容的介绍,我们可以看到Oracle的确是无微不至。有了这个NULL值位置的强制方法,我们在书写SQL时的灵活性和可控性便可进一步得到加强。Good luck.secooler10.01.30-- The End --