嘗試類似:
select StudentID, Dept, CrsNum
from StudentCourse SC
where CrsNum in (101, 201, 202) and
exists
(select null
from StudentCourse SC1
where SC.StudentID = SC1.StudentID and
sc1.CrsNum = 101) and
exists
(select null
from StudentCourse SC2
where SC.StudentID = SC2.StudentID and
sc1.CrsNum in (201,202))
另外,你應該能夠做到這一點,而不與像查詢任何子查詢:
select StudentID,
Sum(case when CrsNum = 101 then 1 else 0) InCrs101,
Sum(case when CrsNum = 201 then 1 else 0) InCrs201,
Sum(case when CrsNum = 202 then 1 else 0) InCrs202
from StudentCourse SC
where CrsNum in (101, 201, 202)
group by StudentID
having Sum(case when CrsNum = 101 then 1 else 0) >= 1 and
(Sum(case when CrsNum = 201 then 1 else 0) >= 1 or
Sum(case when CrsNum = 202 then 1 else 0) >= 1)
你能給數據庫結構,一些樣本數據和例子,什麼是你期望 – Dheer 2010-09-03 06:38:45