我有一个"员工"表。我有一个" Team"包含四列的表,每列包含一个int ID。每个ID代表一名员工。 (垃圾数据可能来自Northwind数据库)
我想做一个Query,它返回一个带有ID的表和Employee数据的串联。 我已经能够使用CoordinatorID,但不知道如何获得多个列。
我想为EstimatorID,ProjectManagerID和&amp ;;的CoordinatorID做些什么。 MarketingSalesmanID。
我无法上传和映像,但是有一个指向我的SQL Server窗口图像的链接,这有助于澄清我想要做的事情。
另外,我怀疑无论如何我都是这么做的。所以如果有人能告诉我一个我可以学习的例子,我会非常感激。我不介意从头开始......简单的例子是值得赞赏的。我还在学习......
员工数据库
ID FirstName LastName Title
1 Nancy Davolio销售代表
2 Andrew Fuller销售副总裁
3 Janet Leverling销售代表
团队数据库
ID TeamName CoordinatorID EstimatorID ProjectManagerID MarketingSalesmanID
1 Red 7 2 8 6
2 Green 1 7 3 3
3 Blue 9 5 3 4
SELECT TOP (3) [Employee].[TeamTbl].[TeamName], [Employee].[TeamTbl].[CoordinatorID],
CONCAT( [Employee].[EmployeeTbl].[FirstName],' ', [Employee].[EmployeeTbl].[LastName],', ', [Employee].[EmployeeTbl].[Title]) As CoordinatorIDName
FROM [Employee].[TeamTbl]
INNER JOIN [Employee].[EmployeeTbl] ON [Employee].[TeamTbl].[CoordinatorID]=[Employee].[EmployeeTbl].[ID];
答案 0 :(得分:2)
您需要使用不同的别名多次加入Employee表:
SELECT TOP (3)
[Employee].[TeamTbl].[TeamName],
CONCAT( c.[FirstName],' ', c.[LastName],', ', c.[Title]) As CoordinatorName
CONCAT( e.[FirstName],' ', e.[LastName],', ', e.[Title]) As EstimatorName
CONCAT( p.[FirstName],' ', p.[LastName],', ', p.[Title]) As ProjectManagerName
CONCAT( m.[FirstName],' ', m.[LastName],', ', m.[Title]) As MarketingSalesmanName
FROM
[Employee].[TeamTbl]
INNER JOIN
[Employee].[EmployeeTbl] c ON [Employee].[TeamTbl].[CoordinatorID]=c.[ID]
INNER JOIN
[Employee].[EmployeeTbl] e ON [Employee].[TeamTbl].[EstimatorID]=e.[ID]
INNER JOIN
[Employee].[EmployeeTbl] p ON [Employee].[TeamTbl].[ProjectManagerID]=p.[ID]
INNER JOIN
[Employee].[EmployeeTbl] m ON [Employee].[TeamTbl].[MarketingSalesmanID]=m.[ID]