【发布时间】:2013-07-31 08:04:23
【问题描述】:
我正在尝试使用以下查询连接两个表 Table1 和 Table2。我想要 Table1 中的所有列(和行)和 Table2 中的列“BasedOnDate”。 问题是,在左连接后,“BasedOnDate”列中的所有值都设置为 1,而不是在两个表中匹配的行上(比如 Table1 中的第 1 行和 Table2 中的第 1 行。有人可以评论问题出在哪里?
我现在使用的查询:
SELECT Table1.*, Table2.BasedOnDate
FROM Table1 LEFT JOIN Table2 ON (Table1.Name = Table2.Name) AND
(Table1.[Date-4] = Table2.[Date-4]) AND
(Table1.[Date-3] = Table2.[Date-3]) AND
(Table1.[Date-2]= Table2.[Date-2]) AND
(Table1.[Date-1] = Table2.[Date-1]) AND
(Table1.ID = Table2.ID) AND
(Table1.Site = Table2.Site);
Table2:
Site ID Date-1 Date-2 Date-3 Date-4 Name BasedOnDate
00001 201 3/30/2011 4/6/2011 4/3/2011 4/6/2011 Name-1 1
Table1:
Site ID Date-1 Date-2 Date-3 Date-4 Name
00001 201 3/30/2011 4/6/2011 4/3/2011 4/6/2011 Name-1
00001 101 5/21/2011 5/28/2011 5/21/2011 5/28/2011 Name-2
【问题讨论】:
-
是
Table2.Date-4应该是日期减四还是您的列名称Date-4。对于后者,您需要将这些列括在括号中,例如Table2.[Date-4] -
您确定在某处没有 where 子句吗?您能否在问题中包含实际结果?
-
我对“左连接后'BasedOnDate'列中的所有值都被设置为1”这一说法感到困惑,因为示例数据中的值为1?
-
In Access
Name是一个保留字,所以应该用方括号括起来。