简体   繁体   English

SQL连接查询不返回任何内容

[英]SQL join query not returning anything

I am trying to make a query using a join statement. 我正在尝试使用join语句进行查询。 This is what I have: 这就是我所拥有的:

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) 

But when I use this (I added another parameter in the WHERE clause): 但是,当我使用它时(我在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)

There's no result even if I have a row in my Customer table that matches the Person.Id and that specific row has a field in which RoleType="XX" and LocType=3. 即使我的客户表中有一行与Person.Id相匹配,并且该特定行具有一个RoleType =“ XX”和LocType = 3的字段,也没有结果。

UPDATE: fixed it, but now i am having a problem.. i did this: 更新:修复它,但是现在我有一个问题..我做到了:

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' 

Replace the INNER JOIN with a LEFT OUTER JOIN in your second SELECT to see what is actually being detected in the Customer table, and if it is what you expect. 在第二个SELECT中将INNER JOIN替换为LEFT OUTER JOIN ,以查看“ Customer表中实际检测到的内容以及是否符合您的期望。

Check data types: is, for instance, RoleType a CHAR field (with padding) instead of VARCHAR ? 检查数据类型:例如,RoleType是CHAR字段(带填充)而不是VARCHAR吗?

Should you be joining customerid to personid? 您是否应该将customerid加入personid? Or is there a personid column in the customer table? 还是客户表中有一个personid列? Those may not match up... One of those tables is likely to contain the foreign key. 那些可能不匹配...这些表之一可能包含外键。 Something like: 就像是:

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)  

Depending on how your tables are laid out, this could be 根据表的布局方式,这可能是

Customer.PersonId = Person.Id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM