0

我正在尝试使用 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' 
4

2 回答 2

1

INNER JOIN用 a替换LEFT OUTER JOIN你的第二个SELECT以查看Customer表中实际检测到的内容,以及它是否是你所期望的。

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

于 2012-07-18T08:07:34.170 回答
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
于 2012-07-18T08:12:55.333 回答