0

次のテーブルがあります。

students (studentid, firstname, lastname)
finals (studentid, subjectid, finaldate, mark)
subjects (subjectid, subjectname, semester)

データベースの最終試験を受けた学生 (ID を表示) を知る必要があります。私は次のことをしました:

select studentid
from finals
where subjectid in (select subjectid
                    from subjects
                    where subjectname = 'database');

JOINinsted ofを使用しても同じ結果が得られINますか?

select studentid
from finals f, subjects s
where f.subjectid = s.subjectid
and s.subjectname = 'database';

また、データベースの最終試験を受けたことがない学生 (ID を提示) を知る必要がある場合はどうすればよいでしょうか?

やってることは同じか…

select studentid
from finals
where subjectid not in (select subjectid
                        from subjects
                        where subjectname = 'database');

...これより?

select studentid
from finals f, subjects s
where f.subjectid = s.subjectid
and s.subjectname <> 'database';

ありがとう。

4

1 に答える 1

0

最初の質問に対する答えは一般的に「はい」ですが、適切な結合構文を使用する必要があります。

select studentid
from finals f join
     subjects s
     on f.subjectid = s.subjectid and s.subjectname = 'database';

subjects一般に、テーブル内の重複はjoinバージョンではなくバージョンに表示されるためinです。厳密に同等のものは次のとおりです。

select studentid
from finals f join
     (select distinct subjectid, subjectname
      from subjects s
     ) s
     on f.subjectid = s.subjectid and s.subjectname = 'database';

(Aは近いですが、テーブルselect distinct studentidに重複が存在する場合は削除されます。)finals

2番目の答えは「いいえ」です。正しいクエリは、一致しない行のみを取得left outer joinするフィルターを使用したものです。where

select studentid
from finals f left outer join
     subjects s
     on f.subjectid = s.subjectid and s.subjectname = 'database'
where s.subjectid is not null;
于 2013-09-18T03:52:35.730 に答える