我有两张桌子
IDartist name ----------- ----------- 1 akon 2 50 cent IDsong name IDartist IDauthor IDcomposer IDmusicProducer ------ ---- -------- -------- --------- --------------- 1 lonley 1 2 1 2
我是如何知道哪位艺术家,IS作者,IDcomposer,IDmusicProducer?
我试图这样做而且不起作用:
SELECT songs.`IDsong`, songs.`IDartist`, songs.`name`, songs.`IDgenre`, songs.`IDauthor`, songs.`IDcomposer`, songs.`IDmusicProducer`, artists.`name` As artists_name, songs_genres.`name` As genres_name FROM `songs` LEFT OUTER JOIN `artists` ON (songs.`IDartist` = artists.`IDartist` OR songs.`IDauthor` = artists.`IDartist` OR songs.`IDcomposer` = artists.`IDartist` OR songs.`IDmusicProducer` = artists.`IDartist`) LEFT OUTER JOIN `songs_genres` ON (songs.`IDgenre` = songs_genres.`IDgenre`) WHERE songs.`IDsong`=XXX
答案 0 :(得分:2)
为了获取艺术家,作者,作曲家和制作人的名字,您必须将artists
表加入songs
表中不同的时间。查询应如下所示:
SELECT
s.IDsong, s.name,
a1.name AS artists_name,
a2.name AS author_name,
a3.name AS composer_name,
a4.name AS producer_name,
sg.name AS genres_name
FROM songs AS s
JOIN artists AS a1 ON s.IDartist = a1.IDartist
JOIN artists AS a2 ON s.IDauthor = a2.IDartist
JOIN artists AS a3 ON s.IDcomposer = a3.IDartist
JOIN artists AS a4 ON s.IDmusicProducer = a4.IDartist
JOIN songs_genres AS sg ON s.IDgenre = sg.IDgenre
如果某位唱片可能没有艺术家,作家,作曲家和制作人,那么您将不得不在表格上使用LEFT JOIN
。