我有这个 sql,它可以找到参加过 ID 为 10101 的讲师教授的类(class)部分的(不同的)学生总数。
select count (distinct ID)
from takes
where (course_id, sec_id, semester, year) in
(select course_id, sec_id, semester, year
from teaches
where teaches.ID= 10101);
重写它的另一种或最佳方法是什么。
我们将感谢您的帮助。
最佳答案
为什么不用 ANSI Join?
select
count (distinct t1.ID)
from
takes as t1
inner join teaches as t2 on
t1.course_id=t2.course_id and
t1.sec_id=t2.sec_id and
t1.semester=t2.semester and
t1.year=t2.year
where
t2.ID= 10101
关于mysql - 如何以更好的方式重写这个嵌套查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11576279/