1

我正在尝试使用下面的查询连接两个表 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
4

1 回答 1

1

当我重新创建您的情况时,我可以确认使用正确的语法查询有效:检索所有Table1加连接Table2的列BasedOnDate。查询返回的不匹配nullBasedOnDate.

SITE ID     DATE1           DATE2           DATE3           DATE4           NAME    BASEDONDATE
1    201    March, 30 2011  April, 06 2011  April, 03 2011  April, 06 2011  Name-1  1
1    101    May, 21 2011    May, 28 2011    May, 21 2011    May, 28 2011    Name-2  (null)

你可以在这里查看:http ://sqlfiddle.com/#!6/a56ee/2

如果您需要进一步研究,请发表评论。希望对你有帮助!

于 2013-07-31T00:48:31.483 回答