查出选择了课程2的同学还共同选择了什么课程。Sno是同学编号,Cno是课程。直观的就可以看出选了2课程的同学还共同选了3。但是查询怎么写。
解决方案
40
SELECT Cno FROM(
SELECT a.Sno,a.Cno FROM sc a
JOIN
(SELECT Sno FROM sc WHERE Cno = 2) b
ON a.Sno=b.Sno
AND Cno != 2) a
GROUP BY Cno HAVING COUNT(*)>1;
SELECT a.Sno,a.Cno FROM sc a
JOIN
(SELECT Sno FROM sc WHERE Cno = 2) b
ON a.Sno=b.Sno
AND Cno != 2) a
GROUP BY Cno HAVING COUNT(*)>1;