1

问题是我正在使用一个对另一个表有多个引用的表;基本上,我为一首歌曲的原始艺术家存储了三个不同的值,如果它以另一位艺术家为特色,以及它是否是混音。这些链接中的每一个都指向艺术家表,我想要的是能够为这些列中的每一个返回艺术家姓名(没有其他数据)。到目前为止我有这个:

SELECT `songName`, `songTrackNumber`, `artist`.`artistName`,
songFeaturingArtist, songRemixByArtist
FROM `song`
LEFT JOIN artist ON songArtist = artistID

我不完全确定如何将其他两个字段从他们的数字 ID 转换为艺术家的名字。任何指针将不胜感激。

4

2 回答 2

3

只需继续添加连接,但通过使用表别名来让 MySQL 满意:

SELECT `songName`, `songTrackNumber`, 
    `a1`.`artistName` AS mainArtistName,
    `a2`.`artistName` AS songFeaturingArtistName,
    `a3`.`artistName` AS songRemixByArtistName
FROM `song`
LEFT JOIN artist a1 ON songArtist = a1.artistID
LEFT JOIN artist a2 ON songFeaturingArtist = a2.artistID
LEFT JOIN artist a3 on songRemixByArtist = a3.artistID

为清楚起见,您可以将别名更改为比简单的数字后缀更有用的东西:)

于 2013-01-18T14:35:17.493 回答
3

三次加入表Artist中的表以获取表中所有其他列的歌曲。songartistName

SELECT  a.songName, 
        a.songTrackNumber, 
        b.artistName AS ArtistName,
        c.artistName AS FeaturingArtist,
        d.artistName AS RemixByArtist
FROM    song a
        LEFT JOIN artist b ON a.songArtist = b.artistID
        LEFT JOIN artist c ON a.songFeaturingArtist = c.artistID
        LEFT JOIN artist d ON a.songRemixByArtist = d.artistID
于 2013-01-18T14:36:15.133 回答