1、ORDER BY 中关于NULL的处理
缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前。
当然,你也可以使用nulls first 或者nulls last 语法来控制NULL的位置。 Nulls first和nulls last是Oracle Order by支持的语法 如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc 还是 desc) 如果Order by 中指定了表达式Nulls last则表示null值的记录将排在最后 (不管是asc 还是 desc) 使用语法如下:
--将nulls始终放在最前 select * from 表名 order by 列名 nulls first
--将nulls始终放在最后 select * from 表名 order by 列名 last
2、几种排序的写法 单列升序:
select<column_name> from <table_name> order by <column_name>; (默认升序,即使不写ASC)
单列降序:select <column_name> from <table_name> order by <column_name> desc;
多列升序:select <column_one>, <column_two> from <table_name> order by <column_one>, <column_two>;
多列降序:select <column_one>, <column_two> from <table_name> order by <column_one> desc, <column_two> desc;
多列混合排序:select <column_one>, <column_two> from <table_name> order by <column_one> desc, <column_two> asc;
sql 无规律排序
select * from ( select 1 a,1 b from dual union all select 1 a,2 b from dual union all select 10 a,2 b from dual union all select 100 a,2 b from dual ) xxx order by decode(a,100,0,a)
等价如下语句
select 100 a,2 b from dual union all select * from ( select 1 a,1 b from dual union all select 1 a,2 b from dual union all select 10 a,2 b from dual ) xxx