根據條件取不同列中的值來排序
有時排序的要求會比較復雜,比如:領導對工資在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;
執行結果如下: