使用not in排除結果 你可以使用not in關鍵字來獲得明確地不被包含在另一個結果組中的結果。例如,我想要通過下面的代碼來返回metallica在“and justice for all”專輯中不包含單詞“justice”的歌曲:
select song_name from album where album_name = ‘and justice for all’ and band_name = ‘metallica’ and song_name not in (select song_name from lyric where song_lyric like ‘%justice%’);
在前面的sql代碼中,我選擇了metallica的“and justice for all,”專輯中的所有歌曲,接著是帶有歌詞中帶有“justice”所有歌曲,最后從在lyric結果組中沒有出現的album結果組返回了所有歌曲。較之于返回兩個查詢并使用代碼來比較數組,你通過一個單獨的聲明就可以得到確切的結果。
select album.song_name from album where album.band_name = ‘metallica’ and exists (select cover.song_name from cover where cover.band_name = ‘damage, inc.’ and cover.song_name = album.song_name);
select albuminfo.album_name from albuminfo where albuminfo.band_name = ‘metallica’ and album_tracks <> (select count(*) from album where album.album_name = albuminfo.album_name);
select albuminfo.album_name, album_tracks, (select count(*) from album where album.album_name = albuminfo.album_name) from albuminfo where albuminfo.band_name = ‘metallica’;