我想获得一张在他们的专辑中至少有一首歌的歌手名单:
CREATE TABLES singers
id_singer
name_singer
我们用歌手列出歌曲:
CREATE TABLE songs
id_song
id_singers // this is the id_singer in singers table
如果我们有2首或更多歌手的歌曲
,则使用此表格CREATE TABLE songs_join
id_sj
id_song_join // this is the id_song on songs table
id_singer_join // this is the id_singer in singers tables
因此,我们需要寻找一位歌手,他的ID存在于歌曲列表中(singer.id_singer
= songs_id_singers
),songs.id_song
应该等于songs_join.id_song_join
查询。
提前致谢
答案 0 :(得分:1)
使用
SELECT * FROM singers AS s
JOIN songs AS so ON so.id_signer = s.id_singer
JOIN songs_join AS sj ON sj.id_signer_join = s.id_singer
AND sj.id_song_join = so.id_song
答案 1 :(得分:1)
尝试此查询。
SELECT name_singer
FROM singers
INNER JOIN songs ON songs.id_singers = singers.id_singer
INNER JOIN songs_join ON songs_join.id_singer_join = singers.id_singer;