我有两张表,如下所示
empid firstName lastName lstatus
1 Mark Rowen 1
2 John Griffin 1
3 Derick Rose 1
EmpPers
----------
empPersSeq empID perTypeID PersTypeValue
1 1 1 Good
2 1 2 Fair
3 1 3 Good
4 2 1 Fair
5 2 2 Fair
6 2 2 Fair
如何从EmpMaster中获取EmpPers表中没有perTypeID = 3条记录的所有EmpId列表?
提前致谢
答案 0 :(得分:2)
select *
from EmpMaster e
where not exists (
select *
from EmpPers p
where p.empID = e.empID and p.perTypeID = 3
)
答案 1 :(得分:2)
我相信一个NOT EXISTS可以解决这个问题:
SELECT
em.empid
FROM EmpMaster em
WHERE NOT EXISTS (
SELECT NULL
FROM EmpPers
WHERE empID = em.empid AND perTypeID = 3
);
答案 2 :(得分:0)
如果我理解正确,你可以这样做:
SELECT e.empid FROM EmpMaster e
WHERE (SELECT COUNT(*)
FROM EmpPers p
WHERE e.empid = p.empID AND p.empPersSeq = 3) = 0
答案 3 :(得分:0)
您可以加入2个表,在empid上聚合,并使用HAVING子句仅选择perTypeID从不= 3的empid。
select a.empid
from EmpMaster a
left join EmpPers b
on a.empid = b.empid
group by a.empid
HAVING max(case when b.perTypeID = 3 then 1 else 0 end) =0