我想根据IN子句示例中的顺序来命令sql查询的结果集:
select * from Employee where EmpID='Eng' and DeptID IN ('Software','Mech','Aero','Civil'),
对于上述查询我想按顺序订购结果集1.软件,2。Mech,3。Aero,4。Civil
答案 0 :(得分:5)
在Oracle中,使用instr()
或case
:
select *
from Employee
where EmpID='Eng' and DeptID IN ('Software','Mech','Aero','Civil')
order by instr('Software,Mech,Aero,Civil', DeptId)
这也适用于DB2。
case
表单也适用于两个数据库,但更长:
order by (case DeptID when 'Software' then 1
when 'Mech' then 2
when 'Aero' then 3
when 'Civil' then 4
end)
答案 1 :(得分:3)
您可以维护虚拟列中的顺序并使用表中的输入或使用with子句
with subject as
(select 1 ord,'Software' as sub from dual
union
select 2 ord,'Mech' as sub from dual
union
select 3 ord,'Aero' as sub from dual
union
select 4 ord,'Civil' as sub from dual)
select e.* from Employee,subject
where EmpID='Eng'
and DeptID=subject.sub
order by subject.ord