I'm looking for all names and URLs of artists that a user with user_id 57 has stored.
Table "artists"
id | name | url
---+--------------+--------
01 | Luke Combs | url-1
02 | Jason Aledan | url-2
03 | Upchurch | url-3
04 | Lee Brice | url-4
05 | Khalid | url-5
Lookup table user_artists
:
user_id | artist_id
---------+-----------
02 | 05
15 | 01
37 | 01
57 | 03
57 | 01
28 | 02
Desired outcome for user_id=57:
name | url
------------+--------
Luke Combs | url-1
Upchurch | url-3
My attempt:
SELECT name, url, user_id
FROM artists, user_artists
INNER JOIN user_artists.user_id ON artists.name
WHERE user_id = 57;
The SQL queries seem so confusing to me. I'm not even sure if inner join is the right one. I spent the whole afternoon googling these queries and finally came with this via a SQL generator. Any help, thanks much!
You didn't mention what concrete RDBMS this is for - and the various RDBMS are not 100% compatible in how they "interpret" the SQL standards - but I guess something like this should work in just about any RDBMS:
SELECT name, url, user_id
FROM artists a
INNER JOIN user_artists ua ON ua.artist_id = a.id
WHERE ua.user_id = 57;
Basically, you take those rows from user_artists
where the user_id
matches your desired value, and then you join the artists
table via the "link" between user_artists.artist_id
to the artist.id
column - and that produces your desired output.