简体   繁体   中英

SQL join query not returning anything

I am trying to make a query using a join statement. 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):

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.

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.

Check data types: is, for instance, RoleType a CHAR field (with padding) instead of VARCHAR ?

Should you be joining customerid to personid? Or is there a personid column in the customer table? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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