我根本不擅长SQL
我在MS访问数据库中有两个表
|ID| Name |Surname|Postion| |EmpID|ManID|
---------------------------- -------------
|1 |Scrooge|McDuck |Manager| |3 |1 |
|2 |Daisy |Duck |Manager| |7 |1 |
|3 |Donald |Duck |Support| |6 |2 |
|4 |Minny |Mouse |Support| |4 |2 |
|5 |Mickey |Mouse |Support| |2 |1 |
|6 |Goofy | |Support| |1 |2 |
|7 |Pluto | |Support| |5 |2 |
|8 |Huey |Duck |Support|
|9 |Dewey |Duck |Support|
|10|Louie |Duck |Support|
我需要编写一个SQL语句来生成以下输出
| Name |Surname|Postion|Manager Name|Manager Positon|
------------------------
|Donald |Duck |Support|Scrooge |Manager
|Pluto | |Support|Scrooge |Manager
|Goofy | |Support|Daisy |Manager
|Minny |Mouse |Support|Daisy |Manager
|Daisy |Duck |Support|Scrooge |Manager
|Scrooge|McDuck |Manager|Daisy |Manager
|Mickey |Mouse |Manager|Daisy |Manager
到目前为止,我的代码看起来像这样(我一直在网上看看它是如何完成的以及为什么这样做)
SELECT Employee.Name,Employee.Surname,Employee.Position,Manager.Name as ManagerName
FROM Employee
INNER JOIN Stafflink ON Employee.ID=Stafflink.EmpID
INNER JOIN Employee Manager ON Manager.ID=Stafflink.ManID;
我知道问题已在Sql table output
中得到解答但它似乎无法正常工作并产生错误:
语法错误(缺少运算符)
非常感谢任何协助。
答案 0 :(得分:3)
MS Access对多个JOIN
的非典型要求,它们被包含在嵌套的()
组中,如:
FROM
((t1 INNER JOIN t2 ON t1.id = t2.id)
INNER JOIN t3 ON t2.id = t3.id)
您的FROM
条款应表示为:
SELECT Employee.Name,Employee.Surname,Employee.Position,Manager.Name as ManagerName
FROM ((
Employee
INNER JOIN Stafflink ON Employee.ID=Stafflink.EmpID)
INNER JOIN Employee Manager ON Manager.ID=Stafflink.ManID);