SQL SELECT of three tables

I have three tables:

Map_table - id - content_id - from_user_id - to_user_id - timestamp User_table - id - name Content_table - id - title 

For example: I want to select rows from Map_table, where Map_table.to_user_id = 1

and also provide User_table.name where Map_table.from_user_id = User_table.id

and also provide Content_table.title, where Map_table.content_id = Content_table.id

Map_table.content_id may be null and therefore not display in Content_table

If I got a ton of answers and still tearing my hair to get the results I need. Can any SQL guru see a simple solution. Potential JOINs are needed to fry my brain.

Any help would be greatly appreciated. For the sake of my scalp, by the way;)

+4
source share
3 answers
 SELECT mt.*, ut.name, ct.title FROM Map_table mt INNER JOIN User_table ut on mt.from_user_id = ut.id LEFT JOIN Content_table ct on mt.content_id = ct.id WHERE mt.to_user_id = 1 
+9
source
 SELECT m.id,m.content_id,m.from_user_id,m.to_user_id,m.timestamp,u.name,c.title FROM Map_table m INNER JOIN User_table u ON u.id = m.from_user_id LEFT OUTER JOIN Content_table c ON c.id = m.content_id WHERE m.to_user_id = 1 
+3
source
 SELECT mt.*, ut1.name FROM map_table mt inner join user_table ut1 on mt.from_user_id = ut1.id inner join user_table ut2 on mt.to_user_id = ut2.id where mt.to_user_id = 1 

To do this, you need to join user_table twice.

0
source

All Articles