0

我有以下表格

  • 学生(sid,姓名,年龄)
  • 课程(cid,cname,持续时间)
  • 注册(sid,cid,日期)

查找所有参加过 sid = 1 的课程的 sid。

select sid from enroll where cid in (select cid from enroll where sid=1) 
group by sid having count(*)=(select count(*) from enroll where sid=1) 
minus (select sid from student where sid=1);
  1. 第一个 count(*) 指的是什么?
  2. 是否有更简单的查询版本?
4

2 回答 2

0

第一个COUNT(*)是指属于特定的行数sid

重新格式化:

SELECT sid
  FROM enroll
 WHERE cid IN (SELECT cid FROM enroll WHERE sid=1) 
 GROUP by sid
HAVING COUNT(*) = (SELECT COUNT(*) FROM enroll WHERE sid=1) 
MINUS (SELECT sid FROM student WHERE sid=1);

您当然可以使用以下方法避免 MINUS:

SELECT sid
  FROM enroll
 WHERE cid IN (SELECT cid FROM enroll WHERE sid=1)
   AND sid != 1
 GROUP by sid
HAVING COUNT(*) = (SELECT COUNT(*) FROM enroll WHERE sid=1) 
于 2012-10-05T05:49:53.920 回答
0

试试这个

SELECT s.sid
FROM student s, enroll e
WHERE s.sid = e.sid
AND e.cid IN (SELECT cid FROM course WHERE sid=1)
AND s.sid <> 1
于 2012-10-05T06:03:41.737 回答