Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 SQL Server:内部联接返回不正确的行_Sql Server 2008 - Fatal编程技术网

Sql server 2008 SQL Server:内部联接返回不正确的行

Sql server 2008 SQL Server:内部联接返回不正确的行,sql-server-2008,Sql Server 2008,我有两张桌子,一人一桌。我想将person表连接到表1,其中foodId是2 但是,当我进行内部连接时,它只是连接foodId为1的记录 人: 表1: 我尝试的查询是: SELECT * FROM Person p INNER JOIN Table1 t ON p.id = t.personId AND foodId = 2 我还尝试: SELECT * FROM Person p INNER JOIN Table1 t ON p.id = t.personId WHERE t.foodId

我有两张桌子,一人一桌。我想将person表连接到表1,其中foodId是2

但是,当我进行内部连接时,它只是连接foodId为1的记录

人:

表1:

我尝试的查询是:

SELECT *
FROM Person p
INNER JOIN Table1 t ON p.id = t.personId AND foodId = 2
我还尝试:

SELECT *
FROM Person p
INNER JOIN Table1 t ON p.id = t.personId 
WHERE t.foodId = 2
这两个查询都显示空结果


如有任何建议,将不胜感激

也许你需要左连接而不是内连接

SELECT * FROM Table1 t LEFT JOIN Person p ON p.id = t.personId WHERE t.foodId = 2

进行内部联接时,将只显示两个表中存在的记录

我喜欢这张照片 SQLFiddle似乎已经死了。。。无法连接-因此,张贴在这里的所有荣耀

设置如原始问题所示:

DECLARE @Person TABLE (id INT, fName VARCHAR(20), lName VARCHAR(50))

INSERT INTO @Person
        (id, fName, lName)
VALUES
        (1, -- id - int
         'John', -- fName - varchar(20)
         'Smith'  -- lName - varchar(50)
         )

DECLARE @Table1 TABLE (id INT, PersonId INT, FoodID INT, T1Date DATE)

INSERT INTO @Table1
        (id, PersonId, FoodID, T1Date)
VALUES
        (1, -- id - int
         1, -- PersonId - int
         1, -- FoodID - int
         '20141028'  -- T1Date - date
         ), (2, 1, 2, '20141027')
SELECT *
FROM @Person p
INNER JOIN @Table1 t ON p.id = t.personId 
WHERE t.foodId = 2
原始问题中显示的问题2:

DECLARE @Person TABLE (id INT, fName VARCHAR(20), lName VARCHAR(50))

INSERT INTO @Person
        (id, fName, lName)
VALUES
        (1, -- id - int
         'John', -- fName - varchar(20)
         'Smith'  -- lName - varchar(50)
         )

DECLARE @Table1 TABLE (id INT, PersonId INT, FoodID INT, T1Date DATE)

INSERT INTO @Table1
        (id, PersonId, FoodID, T1Date)
VALUES
        (1, -- id - int
         1, -- PersonId - int
         1, -- FoodID - int
         '20141028'  -- T1Date - date
         ), (2, 1, 2, '20141027')
SELECT *
FROM @Person p
INNER JOIN @Table1 t ON p.id = t.personId 
WHERE t.foodId = 2
该查询的输出:


这里肯定有别的事情发生了——或者你把设置过于简单,使它不再工作了。但是您的查询2确实返回了一行—您希望返回的行。

要么您没有真正向我们显示您的实际表,要么您没有向我们显示正确的数据—提供了这些数据,查询2确实返回了1,'John','Smith',1,2,'2014-10-28'作为其答案……这就是我认为它应该返回的内容。在我的第二个查询中,如果我没有包含WHERE子句,那么两条记录都应该返回,对吗?第一个查询的格式似乎有点奇怪。如果您在p.id=t.personId和t.foodId=2上尝试内部联接Table1 t,这可能会解决问题,因为第二个条件没有引用表别名。如果它有效的话,它应该仍然会给出相同的结果。我让它起作用了。我在Sql Server中的第二个查询中有一个输入错误,但在这里输入的是正确的。那么,当从每个表中选择*时,您能得到正确的结果吗?我简化了这个问题,但我成功了。在我加入时,我是用p.id=t.id而不是t.personId加入的。@安德鲁:啊哈!-这当然行不通:-