SQL连接查询没有返回任何内容

时间:2012-07-18 08:04:59

标签: c# sql join inner-join

我正在尝试使用join语句进行查询。这就是我所拥有的:

SELECT Person.Id,  
      Person.AddressLine1, Person.AddressLine2, Person.Name, 
      Person.City
  FROM Person WITH (NOLOCK) INNER JOIN
      Customer ON 
      Customer.Id = Person.Id
  WHERE (Person.IsRegular = 1) 

但是当我使用它时(我在WHERE子句中添加了另一个参数):

SELECT Person.Id,  
      Person.AddressLine1, Person.AddressLine2, Person.Name, 
      Person.City
  FROM Person WITH (NOLOCK) INNER JOIN
      Customer ON 
      Customer.Id = Person.Id
  WHERE (Person.IsRegular = 1) AND
      (Customer.RoleType = 'XX') AND 
      (Customer.LocType = 3)

即使我的Customer表中有一行与Person.Id匹配且该特定行包含RoleType =“XX”且LocType = 3的字段,也没有结果。

更新: 修好了,但现在我遇到了问题..我这样做了:

SELECT Person.Id, Person.AddressLine1, Person.AddressLine2, Person.Name, Person.City 
FROM Person WITH (NOLOCK) 
INNER JOIN Customer ON Customer.Id = Person.Id WHERE (Person.IsRegular = 1) AND (Customer.RoleType = 'XX') AND (Customer.LocType = 3) 
AS xxx ON xxx.Id=1... it says:incorrect syntax near the keyword 'AS' 

2 个答案:

答案 0 :(得分:1)

INNER JOIN替换为您的第二个LEFT OUTER JOIN中的SELECT,以查看Customer表中实际检测到的内容,以及它是否符合预期。< / p>

检查数据类型:例如,RoleType是CHAR字段(带填充)而不是VARCHAR

答案 1 :(得分:0)

您是否应该加入customerid到personid?或者客户表中是否有personid列?那些可能不匹配...... 其中一个表可能包含外键。 类似的东西:

SELECT Person.Id,         
Person.AddressLine1, 
Person.AddressLine2, Person.Name,        
Person.City   
FROM Person WITH (NOLOCK) INNER JOIN Customer ON
Customer.Id = Person.CustomerId   
WHERE (Person.IsRegular = 1)  

根据表的布局方式,这可能是

Customer.PersonId = Person.Id