一、多字段排序
eg: select empno,deptno,sal,ename,job
from emp
order by 2 asc,3 desc;
注:1.其中2、3代表取其中位置为2、3的参数名;
2.多字段排序用逗号隔开即可;
二、 函数 translate(expr,from_string,to_string)
eg:translate(''ab 你好 bcadefg','abcdefg','1234567')
结果 12 你好 2314567
1.如果to_string为空,则返回空值;
2.如果to_string对应的位置没有字符,删除from_string中列出的字符将会被消掉;
3.我们一般应用场景:当列值为 data
7369 sajda
54343 dadaw
43544 dawd
时需要排序,那么就要消掉其中的字母或者数字在经行排序;
eg:translate(data,'- 0123456789','-'); 这样巧妙的删掉了空格和数字('-' 不加会为null,全删)
eg:想排序数字同理;
三、处理排序的空值
1.一般情况值为空排序默认在后面,如果想把控制显示在前面,使用nvl(data,-1);
(这种方法需要对列类型及保存的数据有所了解,数据有变化,语句就得重新维护)
2.空值在前关键字 NULLS FIRST
eg:order by 3 NULLS FIRST;
3.空值在后关键字 NULLS LAST
eg:order by 3 NULLS LAST;
四、根据条件取不同的列中的值来排序
eg:取工资在1000 到 2000 之间的员工排在前排序,剩余区间的工资在后排序;
1.select empno as 编码
ename as 姓名,
CASE WHEN sal >=1000 and sal< 2000 THEN 1 ELSE 2 END AS 级别,
sal as 工资
where deptno = 30
order BY 3,4;
结果:可以拟造一个列先排,完后在排另外一列
另一种写法:
2.select empno as 编码,
ename as 姓名,
sal as 工资
from emp
where deptno = 20
order by CASE WHEN sal >=1000 and sal< 2000 THEN 1 ELSE 2 END,3;