Skip to content
Advertisement

Joining INTO many-to-many table (From Primary Key Table)

This is how far i have gotten, though i don’t think it can be done with 1 SQL-statement, i just want to confirm whether or not it is possible to do this with ONLY 1 statement:

users table with foreignkey to users_mentors:

users_mentors table (in a many-to-many relationship with communes):

communes table (in a many-to-many relationship with users_mentors):

mentor_geographies table (the m2m table that has FK to communes & users_mentors):

Is it possible to get all rows from users_mentors and a list of all their commune.type‘s, IF THEY EXIST (if mentor_geographies is empty, i want empty list of commune.type). In all cases i want the user.

Advertisement

Answer

If you want all users, use left join:

I would also recommend that you use table aliases. They make the queries easier to write and to read:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement