从两个表设置单个内部联接非常简单,如下代码。
SELECT A.[Col1], A.[Col2], A.[Col3], A.[Col4], B.[ColJ], B.[ColK]
FROM Table_A AS A
INNER JOIN Table_B AS B
ON A.[Col1] = B.[ColJ]
但是,所讨论的表非常复杂,将需要多个联接。像这样:
SELECT A.[Col1], A.[Col2], A.[Col3], A.[Col4], B.[ColJ], B.[ColK]
FROM Table_A AS A
INNER JOIN Table_B AS B
ON A.[Col1] = B.[ColJ]
INNER JOIN Table_B AS C
ON A.[Col2] = C.[ColK]
但是我无法使其工作。
有什么建议吗?
谢谢
编辑
我的实际代码:
SELECT
j.[AA Number]
,j.[AA Role]
,j.[Project Name] AS [j Project Name]
,j.Series AS [j Series]
,j.[Paper No]
,j.[Task Name] AS [j Task Name]
,j.Amount
,a.[Project Name] AS [TEP_Projects_Tasks_Full Project Name]
,a.[Project Number]
,a.[Task Name] AS [TEP_Projects_Tasks_Full Task Name]
,a.[Expenditure Organization]
FROM
TEP_Payments_Table as j
inner JOIN TEP_Projects_Tasks_Full as a
ON j.[Project Name] = a.[Project Name]
inner join TEP_Projects_Tasks_Full as b
on j.[Task Name] = b.[Task Name]
我得到的结果如下:
如您所见-任务名称未正确加入,它将返回所有任务名称,而不是正确的名称(在本例中为采样)。
如果我使用a而不是b(按照下面的评论),则会出现以下错误:
答案 0 :(得分:2)
您可以尝试选择b.[Task Name] AS [TEP_Projects_Tasks_Full Task Name]
而不是a.[Task Name] AS [TEP_Projects_Tasks_Full Task Name]
SELECT
j.[AA Number]
,j.[AA Role]
,j.[Project Name] AS [j Project Name]
,j.Series AS [j Series]
,j.[Paper No]
,j.[Task Name] AS [j Task Name]
,j.Amount
,a.[Project Name] AS [TEP_Projects_Tasks_Full Project Name]
,a.[Project Number]
,b.[Task Name] AS [TEP_Projects_Tasks_Full Task Name]
,a.[Expenditure Organization]
FROM
TEP_Payments_Table as j
inner JOIN TEP_Projects_Tasks_Full as a
ON j.[Project Name] = a.[Project Name]
inner join TEP_Projects_Tasks_Full as b
on j.[Task Name] = b.[Task Name]