Aykhan Amiraslanli Aykhan Amiraslanli - 2 years ago 158
SQL Question

mysql left join and inner join 3 tables

I have 3 tables: oc_artists, oc_songs, oc_songs_tags

I can select 2 tables with this code:

SELECT * FROM `oc_songs` LEFT JOIN oc_songs_tags ON oc_songs.song_id=oc_songs_tags.song_id
WHERE oc_songs_tags.song_tag IS NULL

Now I need to select data from oc_artists... I tried this questions answer:
MySQL LEFT JOIN 3 tables

And here is my code:

SELECT * FROM oc_songs
LEFT JOIN oc_artists
INNER JOIN oc_songs_tags
ON oc_songs.song_artist_id = oc_artists.artist_id
ON oc_songs_tags.song_id = oc_songs.song_id

But I am getting this error

8 errors were found during analysis.

Unrecognized keyword. (near "ON" at position 131)
Unexpected token. (near "oc_songs_tags" at position 134)
Unexpected token. (near "." at position 147)
Unexpected token. (near "song_id" at position 148)
Unexpected token. (near "=" at position 156)
Unexpected token. (near "oc_songs" at position 158)
Unexpected token. (near "." at position 166)
Unexpected token. (near "song_id" at position 167)

Jim Jim
Answer Source

Your syntax is wrong. Join syntax is:


So that would mean:

SELECT * FROM oc_songs
 LEFT JOIN oc_artists ON oc_songs.song_artist_id = oc_artists.artist_id
 INNER JOIN oc_songs_tags ON oc_songs.song_id = oc_songs_tags.song_id

The simple way to understand JOINS is that they are ALWAYS a conjunction between the primary table listed in FROM and the table you are JOINING. So think of the JOIN clause as a list of conjunctions.

So to join TableA with TableB, TableC, and TableD you might have something like:

  JOIN TableB on TableA.tableb_id = TableB.id  -- this is first join
  JOIN TableC on TableA.tablec_id = TableC.id  -- this is second join
  JOIN TableD on TableA.tabled_id = TableD.id  -- this is third join
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download