gpt4 book ai didi

sql - 有人可以帮我解决 SQL 中的这个连接语句吗?有两个 JOINS 相继

转载 作者:行者123 更新时间:2023-12-04 03:45:02 26 4
gpt4 key购买 nike

在下图中 ([2]) 的第 20 行,您会看到“LEFT JOIN HUB_EMPLOYEES HM”,并且在加入 LINK_EMPLOYEES 后立即出现。

剩下的 HUB_EMPLOYEES 加入了什么? LINK_EMPLOYEES 上的连接在第 21 行指定,但我看不到 HUB_EMPLOYEES 上的左连接如何工作。

如果有人能解释一下,我将不胜感激,我在别处找不到答案。

Relational Diagram image

这是代码:

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

我个人不喜欢这种语法,因为它不是很直观。

关于sql - 有人可以帮我解决 SQL 中的这个连接语句吗?有两个 JOINS 相继,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65320138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com