1. order by 中关于null的处理
缺省处理:Oracle在order by时认为null是最大值, 所以如果是asc升序则排在最后, desc降序则排在最前.
我们可以使用nulls first或者nulls last来控制null的位置
将null放在最前:select * from student order by name asc nulls first
将null放在最后:select * from student order by name desc nulls last
2. 几种排序的写法:
单列升序:select name from student order by name; (默认升序,即使不写asc)
单列降序:select name from student order by name desc;
多列升序:select id, name from student order by id, name;
多列混合排序:select id, name from student order by id desc, name asc; (先按id降序,如果id相同则按name升序)
3. 强制某列排第一:
select * from student order by decode(id, 3, 1, 2), id; // id为3的值排第一