码迷,mamicode.com
首页 > 编程语言 > 详细

根据条件取不同列中的值来排序

时间:2015-10-14 15:47:08      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

有时排序的要求会比较复杂,比如:领导对工资在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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!