我有两个名为emp,dept的表,现在我需要语句的组合意味着
emp表的列
empid | deptid | fulltime | parttime | contracttime
dept table
section id | dept id
以及其他一些不太需要的专栏。
现在在emp表中:
empid | dept id | fulltime | parttime | contrattime
----------------------------------------------------
1 | 1 | 0 | 0 | 1
2 | 1 | 1 | 1 | 0
依旧等等 在dept表中有7行像 section id和dept id如下
1 1 2 1 3 1 4 1 5 1 6 1 7 1这些现在我想要的列 全职兼职合同时间
if 0 0 1 then it should go to section id 1 if 1 1 0 then section id 2 if 1 1 1 then section id 3 if 1 0 1 then section id 4 if 0 1 1 then section id 5 if 1 0 0 then section id 6 if 0 1 0 then section id 7
答案 0 :(得分:0)
您可以在连接中使用CASE语句,如下所示:
select *
from emp e
join dept d
on d.deptid = e.deptid
and (case
when e.fulltime = 0 and e.parttime = 0 and e.contracttime = 1 then 1
when e.fulltime = 1 and e.parttime = 1 and e.contracttime = 0 then 2
when e.fulltime = 1 and e.parttime = 1 and e.contracttime = 1 then 3
when e.fulltime = 1 and e.parttime = 0 and e.contracttime = 1 then 4
when e.fulltime = 0 and e.parttime = 1 and e.contracttime = 1 then 5
when e.fulltime = 1 and e.parttime = 0 and e.contracttime = 0 then 6
when e.fulltime = 0 and e.parttime = 1 and e.contracttime = 0 then 7
else 0
end) = d.sectionid;
这里只有问题可能是性能,因为除非你创建一些复杂的dfunctional索引,否则不会使用索引。