我有这两组数据
CNU RENT NBED CATEGORY
--- ---------- ---------- ----------
101 200 2 3
102 220 3 3
103 180 2 2
104 120 1 1
105 300 3 4
106 350 3 4
107 360 3 4
108 400 4 4
109 500 3 5
110 600 4 5
AID EID CNU SID ADATE HOURS
--- --- --- --- --------- ----------
a01 e08 101 s02 15-MAY-14 3
a02 e03 101 s03 16-MAY-14 4
a03 e02 102 s02 17-MAY-14 2
a04 e09 103 s01 14-MAY-14 3
a05 e06 105 s05 18-MAY-14 5
a06 e06 107 s04 15-MAY-14 3
a07 e03 108 s04 18-MAY-14 4
a08 e09 109 s01 20-MAY-14 5
a09 e08 106 s06 15-MAY-14 3
a10 e10 102 s04 15-MAY-14 2
a11 e02 110 s04 15-MAY-14 4
a12 e10 103 s05 15-MAY-14 5
a13 e05 105 s04 16-MAY-14 4
a14 e10 107 s04 17-MAY-14 3
a15 e09 110 s01 18-MAY-14 3
我希望能够显示3类和4类的EID。
所以输出看起来像这样
EID
---
e03
e08
e10
e02
这是我到目前为止使用的代码
SELECT EID
FROM Assignment15
JOIN Cottage15
ON Assignment15.Cnum = Cottage15.Cnum
WHERE Category=3
AND EXISTS (SELECT Category
FROM Cottage15
WHERE Category=Cottage15.Category
AND Category=4);
结果给了我一个额外的EID
EID
---
e03
e08
e10
e02
e02是额外的。我不确定什么可以调整,但如果我能得到一个适用于此的代码,那将是一个很大的帮助。非常感谢您提前
答案 0 :(得分:0)
您只需使用IN
关键字:
SELECT EID
FROM Assignment15
JOIN Cottage15 ON Assignment15.Cnum = Cottage15.Cnum
WHERE Category IN (3,4)
GROUP BY EID
HAVING COUNT(DISTINCT Category) = 2
请注意,在某些DBMS上,您还可以在USING
条件下使用JOIN
关键字,这将导致
SELECT EID
FROM Assignment15
JOIN Cottage15 ON USING(Cnum)
WHERE Category IN (3,4)
GROUP BY EID
HAVING COUNT(DISTINCT Category) = 2
答案 1 :(得分:0)
您可以使用聚合和having
:
select a.eid
from Assignment15 a join
Cottage15 c
on a.Cnum = c.Cnum
where c.Category in (3, 4)
group by a.eid
having count(distinct c.category) = 2 ;