将第二个表连接到工作示例

时间:2017-02-19 01:18:02

标签: mysql

希望将另一个表加入到现有的工作示例中:

使用SQL:

SELECT u.UserName, u.LastName
     , (e.UserName IS NOT NULL) as user_exists_in_EnrollmentsTbl 
  FROM UsersDataTbl u
  LEFT 
  JOIN EnrollmentsTbl e
    ON u.UserName = e.UserName 
        AND e.ClassName LIKE 'Word%'
 WHERE u.Career = 1 AND Active = 1 ORDER BY u.LastName

我的尝试:

(SELECT u.UserName, u.LastName, d.Station
  , (e.UserName IS NOT NULL) as completedl 
  FROM UsersDataTbl u
  LEFT 
  JOIN EnrollmentsTbl e
    ON u.UserName = e.UserName 
        AND e.ClassName LIKE 'Word%') 
  INNER JOIN UsersDataCareerTbl d 
  ON u.UserName = d.UserName
  WHERE u.Career = 1 AND Active = 1 ORDER BY u.LastName

1 个答案:

答案 0 :(得分:0)

想通了:

SELECT u.UserName, u.LastName, d.Station, (e.UserName IS NOT NULL) as completed 
  FROM (UsersDataTbl u
INNER JOIN UsersDataCareerTbl d ON u.UserName = d.UserName)  
LEFT 
  JOIN EnrollmentsTbl e
    ON u.UserName = e.UserName 
        AND e.ClassName LIKE 'Word%' 
 WHERE u.Career = 1 AND Active = 1 ORDER BY u.LastName