简体   繁体   中英

SQL query for joining information for multiple tables

Here are my 3 tables :

meetings

meeting_id

first_user_id

second_user_id

users

user_id

pic_id

pictures

pic_id

pic_filename

What I need is a SQL query that results in the following record :

meeting_id

first_user_id

second_user_id

first_user_pic_filename

second_user_pic_filename

So far this is what I was thinking :

SELECT meetings.*, pics1.filename first_user_pic_filename, pics2.filename second_user_pic_filename

FROM meetings 

INNER JOIN users users1 ON meetings.first_user = user_id

INNER JOIN users users2 ON meetings.second_user = user_id

INNER JOIN pictures pics1 ON pics1.pic_id = users1.pic_id

INNER JOIN pictures pics2 ON pics2.pic_id = users2.pic_id

I'm not sure exactly what I am doing wrong. Any help would be greatly appreciated.

What I am currently getting is only a few records back, when there should be 10-15. I am wondering if there is an issue with how I am joining.

I suppose you should change

INNER JOIN pictures pics1 ...
INNER JOIN pictures pics2 ...

to

LEFT OUTER JOIN pictures pics1 ...
LEFT OUTER JOIN pictures pics2 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM