如何连接三个具有多个条件的表?

时间:2014-04-06 16:27:12

标签: join

我想加入三个具有多个条件的表。下面是表格结构和预期结果

users_id    users_first_name   
   1           rocky
   2           James
   3           john        

meeting_details_id   meeting_title   users_id  meeting_lead  close_meeting (NO) 
    1               newmeet          3           1                  No 
    2               testmeet         2           2                  No 


Attended_meetings
project_meeting_attendeeid  meeting_details_id  users_id   access_type (attendee) 
  1                             1                 2         attendee  

预期产出:

查询应检查会议是否参加或用户是否符合主要

 meeting_title   creator   meeting_lead     close_meeting (NO) 
 newmeet          john      rocky                  No 
 testmeet         james     james                  No

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT M.meeting_title,U1.users_first_name as creator,U2.users_first_name as meeting_lead,M.close_meeting
FROM MeetingTable M LEFT OUTER JOIN
Users U1 ON M.creator=U1.users_id LEFT OUTER JOIN
Users U2 ON M.meeting_lead=U2.users_id

结果:

MEETING_TITLE   CREATOR     MEETING_LEAD    CLOSE_MEETING
newmeet         john        rocky           No
testmeet        James       James           No

请参阅SQL Fiddle中的结果。