问题是我正在使用一个对另一个表有多个引用的表;基本上我正在为一首歌的原创艺术家存储三个不同的值,如果它是另一个艺术家的特色,并且它是否是混音。每个链接到艺术家表,我想要的是能够为这些列中的每一列返回艺术家名称(没有其他数据)。到目前为止我有这个:
SELECT `songName`, `songTrackNumber`, `artist`.`artistName`,
songFeaturingArtist, songRemixByArtist
FROM `song`
LEFT JOIN artist ON songArtist = artistID
我不完全确定如何将其他两个字段从其数字ID转换为艺术家的名称。任何指针都将非常感激。
答案 0 :(得分: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
为清楚起见,您可以将别名更改为比简单数字后缀更有用的内容:)
答案 1 :(得分:3)
在表格Artist
上加入表格song
三次,以获得桌面歌曲中所有其他列的artistName
。
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