访问多个左连接 - SQL代码

时间:2016-11-28 21:07:06

标签: sql ms-access left-join

我正在尝试在microsoft访问中将三个表连接在一起,允许以Table 1为我的主表的空条目,以便显示所有记录。

我有这个代码只能链接2个表。

SELECT [1].IDCode, [2].GiftDate, [2].FundId__1, [2].fund_name 
FROM 1 LEFT JOIN 2 ON [1].IDCode = [2].IDCode 
WHERE ((([1].IDCode) Is Not Null));

但我想补充第三个。我有这个,但每次我尝试运行它时,都会收到错误(无效操作)。

SELECT [1].IDCode, [2].GiftDate, [2].FundId__1, [2].fund_name, [3].Rep_Type
FROM (1 LEFT JOIN 2 ON [1].IDCode = [2].IDCode) LEFT JOIN 3 ON [1].IDCode = [3].IDCode
WHERE ((([1].IDCode) Is Not Null));

我的代码现在是这样,我仍然收到无效操作。

SELECT [A].IDCode, [A].GiftDate, [A].FundId__1, [A].fund_name, [3].Rep_Type
FROM 
(SELECT [1].IDCode, [2].GiftDate, [2].FundId__1, [2].fund_name
FROM 1
LEFT JOIN 2
ON [1].IDCode = [2].IDCode) A
LEFT JOIN 3
ON [A].IDCode = [3].IDCode
WHERE ((([1].IDCode) Is Not Null));

1 个答案:

答案 0 :(得分:0)

如果1 - > 2 - > 3,那么连接的第三个应该是2.idcode = 3.idcode

SELECT [1].IDCode, [2].GiftDate, [2].FundId__1, [2].fund_name, [3].Rep_Type
FROM (1 LEFT JOIN 2 ON [1].IDCode = [2].IDCode) LEFT JOIN 3 ON [2].IDCode = [3].IDCode
WHERE ((([1].IDCode) Is Not Null));

但是如果连接真的回到1 ......

因为Access对于()并且加入回到2是挑剔的。

所以我不会迷失()我只是创建了一个内联视图来管理连接。

SELECT [A].IDCode, [A].GiftDate, [A].FundId__1, [A].fund_name, [3].Rep_Type
FROM
(SELECT [1].IDCode, [2].GiftDate, [2].FundId__1, [2].fund_name
 FROM 1 
 LEFT JOIN 2 
   ON [1].IDCode = [2].IDCode) A
LEFT JOIN 3 
  ON [A].IDCode = [3].IDCode
WHERE [A].IDCode Is Not Null;