错误连接使用两列相同的ID - MYSQL

时间:2013-11-17 00:52:28

标签: mysql sql join inner-join

出于某种原因,我不能让这个加入工作。我正在寻找的结果是歌曲标题,受欢迎,录制标题,录制者。

这是我的问题:

SELECT Song.title AS `Song Title`, 
PerfArtist.name AS `Made Popular By`, 
Recording.title AS `Recording Title`, 
PerfArtist.name AS `Recorded By`
FROM PerfArtist_Recording
INNER JOIN Recording ON PerfArtist_Recording.Recording_id = Recording.id
INNER JOIN Song_Recording ON Song_Recording.Recording_id = Recording.id
INNER JOIN Song ON Song_Recording.Song_id = Song.id, 
INNER JOIN PerfArtist ON MadePopularBy.PerfArtist_id = PerfArtist_id
INNER JOIN PerfArtist ON PerfArtist_Recording.PerfArtist_id = PerfArtist.id

我得到的错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN PerfArtist ON MadePopularBy.PerfArtist_id = PerfArtist_id
     INNER ' at line 9

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

SELECT Song.title AS 'Song Title', 
    PerfArtist1.`name` AS 'Made Popular By', 
    Recording.title AS 'Recording Title', 
    PerfArtist2.`name` AS 'Recorded By'
FROM 
    PerfArtist_Recording
INNER JOIN
    Recording ON PerfArtist_Recording.Recording_id = Recording.id
INNER JOIN 
    Song_Recording ON Song_Recording.Recording_id = Recording.id
INNER JOIN 
    Song ON Song_Recording.Song_id = Song.id 
INNER JOIN 
    PerfArtist perfArtist1 ON MadePopularBy.PerfArtist_id = perfArtist1.PerfArtist_id
INNER JOIN 
    PerfArtist perfArtist2 ON PerfArtist_Recording.PerfArtist_id = perfArtist2.PerfArtist.id

删除了一个迷路冒号,用单引号替换了背蜱,以澄清列名和别名。

为PerfArtist添加了别名