1

The problem is that I'm using a table that has more than one reference to another table; basically I'm storing three different values for the original artist of a song, if it is featuring another artist, and if it is a remix or not. Each of these link to the artist table and what I want is to be able to return the artist name (no other data) for each of these columns. I have this so far:

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

I'm not entirely sure how to convert the other two fields from their numeric ID to the name of the artist. Any pointers would be greatly appreciated.

2 Answers 2

3

Join table Artist on table song three times to get the artistName for all the other columns on table song.

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
Sign up to request clarification or add additional context in comments.

6 Comments

Brilliant, I wasn't aware you could left join and give the table a different alias. Thank you!
you're welcome. By the way, if these columns (songArtist, songFeaturingArtist, songRemixByArtist) are non-nullable, use INNER JOIN since it is faster than LEFT JOIN.
one more thing, backtick aren't required on the column names even if you are using reserved keyword because an alias was provided on every table names.
Going to use LEFT JOIN because the other columns are usually NULL, but of course important if they contain a value. Thanks for your help :-)
@JW: excellent point about qualifying column references with a table alias (a.artistName). That's the normative pattern for ANY query that references more than one table. A big advantage is that it insulates a working query from breaking (throwing an "ambiguous column" error) when columns are added to tables referenced in the query.
|
3

Just keep adding joins, but by using table aliases to keep MySQL happy:

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

For clarity you could change the aliases to something more useful than a simple numeric suffix :)

1 Comment

Thanks for the speedy answer! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.