标签:
有时排序的要求会比较复杂,比如:领导对工资在1000到2000元之间的员工更感兴趣,于是要求工资在这个范围的员工要排在前面,以便优先查看。
创建测试用表:
CREATE OR REPLACE VIEW v AS SELECT ‘MARTIN‘ AS ename, 950 AS sal FROM DUAL UNION ALL SELECT ‘WERD‘ AS ename, 1250 AS sal FROM DUAL UNION ALL SELECT ‘TERNER‘ AS ename, 1500 AS sal FROM DUAL UNION ALL SELECT ‘ALLEN‘ AS ename, 1600 AS sal FROM DUAL UNION ALL SELECT ‘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;
执行结果如下:
标签:
原文地址:http://www.cnblogs.com/minisculestep/p/4877434.html