你的位置:首页 > 数据库

[数据库]根据条件取不同列中的值来排序


有时排序的要求会比较复杂,比如:领导对工资在1000到2000元之间的员工更感兴趣,于是要求工资在这个范围的员工要排在前面,以便优先查看。

创建测试用表:

CREATE OR REPLACE VIEW v ASSELECT 'MARTIN' AS ename, 950 AS sal FROM DUALUNION ALLSELECT 'WERD' AS ename, 1250 AS sal FROM DUALUNION ALLSELECT 'TERNER' AS ename, 1500 AS sal FROM DUALUNION ALLSELECT 'ALLEN' AS ename, 1600 AS sal FROM DUALUNION ALLSELECT 'JAMES' AS ename, 2850 AS sal FROM DUAL;SELECT * FROM v;

sql代码如下:

SELECT * FROM v ORDER BY CASE WHEN sal BETWEEN 1000 AND 2000 THEN 1 ELSE 2 END, sal;

执行结果如下: