1

私は、ID 10101 のインストラクターによって教えられたコースセクションを受講した (個別の) 学生の総数を見つけるこの sql を持っています。

            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);

それを書き直す別の方法または最良の方法は何ですか。

あなたの助けは高く評価されます。

4

2 に答える 2

3

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
于 2012-07-20T09:15:20.433 に答える
0
select count (distinct ta.id)
from takes ta
where EXISTS
(select 1 from teaches te
where te.ID=10101
and te.course_id=ta.course_id
and te.sec_id=ta.sec_id
and te.semester=ta.semester 
and te.year=ta.year)

EXISTS を使用すると、外部クエリが評価されるとすぐに BOOLEAN の true/false が返されます。

于 2012-07-20T09:13:34.353 に答える