我想计算参加2门特定课程的学生人数(crn) 我可以显示所说的sids但由于某种原因我无法计算它们。
我想显示服用crn = 12345和crn = 20976
的学生人数这是我的表
sid | crn | grade
12321 | 12345 | A
12321 | 20976 | B
21008 | 12345 | C
21008 | 20976 | A
21008 | 28469 | D
21090 | 12345 | C
21090 | 20976 | F
这是可行的代码
select sid from takes where crn in (12345 , 20976 ) group by sid HAVING COUNT(*) = 2
这是我试过的代码
select count(sid) from takes where crn in (12345 , 20976 ) group by sid HAVING COUNT(*) = 2
select sid, count(sid) from takes where crn in ( '12345', '20976') GROUP By sid HAVING count(*) > 1
select count(sid) from takes where (select sid from takes where crn in ( '12345', '20976') GROUP By sid HAVING count(*) > 1 )
select sid, count(sid) from takes where (select sid from takes where crn in ( '12345', '20976') HAVING count(*) > 1 ) GROUP BY sid
所以这是我的问题,我怎么能让这个东西工作?我的意思是说某些代码可以计算它可以显示的内容并显示该数字
答案 0 :(得分:1)
你需要一个子查询:
select count(*)
from (select sid
from takes
where crn in (12345 , 20976 )
group by sid
having count(*) = 2
) s;