有人可以帮我处理 SQL 中的这个连接语句吗?有两个 JOINS 相继
Can someone help me with this join statment in SQL? There are two JOINS after each other
在下图中 ([2]) 的第 20 行,您会看到“LEFT JOIN HUB_EMPLOYEES HM”,并且在 LINK_EMPLOYEES.
上加入后立即
HUB_EMPLOYEES 左加入的是什么? LINK_EMPLOYEES 上的连接在第 21 行指定,但我看不到 HUB_EMPLOYEES 上的左连接如何工作。
如果有人能解释一下,我将不胜感激,我在其他地方找不到答案。
这是代码:
SELECT HE.EMPLOYEE_SEQNR, HE.EMPLOYEEID, SE.LASTNAME AS EMPLASTNAME,
isnull(SM.LASTNAME, 'DIRECTOR') AS MNGLASTNAME,
SE.TITLE, SE.COUNTRY, SE.REGION, SE.CITY, SE.POSTALCODE
FROM SAT_EMPLOYEES SE
JOIN HUB_EMPLOYEES HE ON SE.EMPLOYEE_SEQNR = HE.EMPLOYEE_SEQNR
LEFT JOIN HUB_EMPLOYEES HM
JOIN LINK_EMPLOYEES ON HM.EMPLOYEE_SEQNR = LINK_EMPLOYEES.MANAGER_SEQNR
JOIN SAT_EMPLOYEES SM ON HM.EMPLOYEE_SEQNR = SM.EMPLOYEE_SEQNR
ON HE.EMPLOYEE_SEQNR = LINK_EMPLOYEES.EMPLOYEE_SEQNR
相当于:
SELECT HE.EMPLOYEE_SEQNR, HE.EMPLOYEEID, SE.LASTNAME AS EMPLASTNAME,
isnull(SubQ.LASTNAME, 'DIRECTOR') AS MNGLASTNAME,
SE.TITLE, SE.COUNTRY, SE.REGION, SE.CITY, SE.POSTALCODE
FROM SAT_EMPLOYEES SE
JOIN HUB_EMPLOYEES HE ON SE.EMPLOYEE_SEQNR = HE.EMPLOYEE_SEQNR
LEFT JOIN (
SELECT LINK_EMPLOYEES.EMPLOYEE_SEQNR
, SM.LASTNAME
FROM HUB_EMPLOYEES HM
JOIN LINK_EMPLOYEES ON HM.EMPLOYEE_SEQNR = LINK_EMPLOYEES.MANAGER_SEQNR
JOIN SAT_EMPLOYEES SM ON HM.EMPLOYEE_SEQNR = SM.EMPLOYEE_SEQNR
) SubQ
ON HE.EMPLOYEE_SEQNR = SubQ.EMPLOYEE_SEQNR
我个人不喜欢这种语法,因为它不是很直观。
在下图中 ([2]) 的第 20 行,您会看到“LEFT JOIN HUB_EMPLOYEES HM”,并且在 LINK_EMPLOYEES.
上加入后立即HUB_EMPLOYEES 左加入的是什么? LINK_EMPLOYEES 上的连接在第 21 行指定,但我看不到 HUB_EMPLOYEES 上的左连接如何工作。
如果有人能解释一下,我将不胜感激,我在其他地方找不到答案。
这是代码:
SELECT HE.EMPLOYEE_SEQNR, HE.EMPLOYEEID, SE.LASTNAME AS EMPLASTNAME,
isnull(SM.LASTNAME, 'DIRECTOR') AS MNGLASTNAME,
SE.TITLE, SE.COUNTRY, SE.REGION, SE.CITY, SE.POSTALCODE
FROM SAT_EMPLOYEES SE
JOIN HUB_EMPLOYEES HE ON SE.EMPLOYEE_SEQNR = HE.EMPLOYEE_SEQNR
LEFT JOIN HUB_EMPLOYEES HM
JOIN LINK_EMPLOYEES ON HM.EMPLOYEE_SEQNR = LINK_EMPLOYEES.MANAGER_SEQNR
JOIN SAT_EMPLOYEES SM ON HM.EMPLOYEE_SEQNR = SM.EMPLOYEE_SEQNR
ON HE.EMPLOYEE_SEQNR = LINK_EMPLOYEES.EMPLOYEE_SEQNR
相当于:
SELECT HE.EMPLOYEE_SEQNR, HE.EMPLOYEEID, SE.LASTNAME AS EMPLASTNAME,
isnull(SubQ.LASTNAME, 'DIRECTOR') AS MNGLASTNAME,
SE.TITLE, SE.COUNTRY, SE.REGION, SE.CITY, SE.POSTALCODE
FROM SAT_EMPLOYEES SE
JOIN HUB_EMPLOYEES HE ON SE.EMPLOYEE_SEQNR = HE.EMPLOYEE_SEQNR
LEFT JOIN (
SELECT LINK_EMPLOYEES.EMPLOYEE_SEQNR
, SM.LASTNAME
FROM HUB_EMPLOYEES HM
JOIN LINK_EMPLOYEES ON HM.EMPLOYEE_SEQNR = LINK_EMPLOYEES.MANAGER_SEQNR
JOIN SAT_EMPLOYEES SM ON HM.EMPLOYEE_SEQNR = SM.EMPLOYEE_SEQNR
) SubQ
ON HE.EMPLOYEE_SEQNR = SubQ.EMPLOYEE_SEQNR
我个人不喜欢这种语法,因为它不是很直观。