我有以下表格
- 学生(sid,姓名,年龄)
- 课程(cid,cname,持续时间)
- 注册(sid,cid,日期)
查找学过java或rdbms的同学的sids
select sid
from enroll e,
course c
where (c.cname like 'java' and c.cid=e.cid)
or (c.cname like 'rdbms' and c.cid=e.cid);
或者:
select sid
from enroll e,
course c
where cname like 'java' and c.cid=e.cid
union
select sid
from enroll e,
course c
where cname like 'rdbms' and c.cid=e.cid;
第二种选择:
select sid
from enroll
where cid in (select cid
from course
where cname like 'java'
or cname like 'rdbms');
以上所有问题都让我得到了预期的结果。
为了找到学过 java和rdbms的学生的 sids ,我只是修改了所有 3,但下面的第 1 和第 3 个查询不起作用。
select sid
from enroll e,
course c
where (c.cname like 'java' and c.cid=e.cid)
and (c.cname like 'rdbms' and c.cid=e.cid);
选择:
select sid
from enroll e,
course c
where cname like 'java' and c.cid=e.cid
intersect
select sid
from enroll e,
course c
where cname like 'rdbms' and c.cid=e.cid;
第二种选择:
select sid
from enroll
where cid in (select cid
from course
where cname like 'java' and cname like 'rdbms');