user2059544
user2059544

Reputation: 23

SQL LEFT JOIN IS NULL over multiple columns

I am using Microsoft Access 2007 and found that this SQL does not have the EXCEPT operator. I learned that I can use LEFT JOIN and IS NULL to perform this operation. I almost have it but can't quite put my thumb on it.

I have two tables, Table A has data of a personID, foodID, and timePURCHASED. A person with an ID buys food of ID at a certain time. Table B is a subset of that. (It has selected rows from Table A.)

My goal is to do: Table A EXCEPT Table B.

Table A

personIDfoodIDtimePURCHASED             
1            1         2/6/2012 1:00:00 PM       
1            4         2/6/2012 6:00:00 PM       
2            3         2/7/2012 8:00:00 PM       
3            1         2/6/2012 9:00:00 PM       
3            3         2/6/2012 4:00:00 PM       

Table B

personIDfoodIDtimePURCHASED             
1            1         2/6/2012 1:00:00 PM       
3            3         2/6/2012 4:00:00 PM       

GOAL

personIDfoodIDtimePURCHASED             
1            4         2/6/2012 6:00:00 PM       
2            3         2/7/2012 8:00:00 PM       
3            1         2/6/2012 9:00:00 PM       

Here is my code I started:

SELECT A.personID, A.foodID, A.timePURCHASED  
FROM A  
LEFT JOIN B ON (A.personID = B.personID)   
WHERE ((B.personID) Is Null);

I understand that this will result in my GOAL table not having any matching personID (1, 3) and leave behind personID not matched (2). I understand that I need to consider all three columns at the same time and found that I can use the AND operator in my LEFT JOIN:

SELECT A.personID, A.foodID, A.timePURCHASED  
FROM A  
LEFT JOIN B ON (A.personID = B.personID)  
AND (A.foodID = B.foodID)  
AND (A.timePURCHASED = B.timePURCHASED)  
WHERE (((B.personID) Is Null)  
AND ((B.foodID) Is Null)  
AND ((B.timePURCHASED) Is Null));

So far this has not worked and I am looking for some advice. I apologize for the format, TLDR, or if this is something easier to answer. I am a novice and learning SQL. Thank you for any assistance.

Upvotes: 2

Views: 8002

Answers (1)

trptcolin
trptcolin

Reputation: 2340

Your second query generally looks good. You don't need all 3 IS NULL checks, though, since you don't have any nulls in your input set. You can also get rid of all those parentheses.

SELECT A.personID, A.foodID, A.timePurchased 
  FROM A 
  LEFT JOIN B 
  ON A.personID = B.personID
    AND A.foodID = B.foodID
    AND A.timePurchased = B.timePurchased
  WHERE B.personID is null;

This works perfectly (meaning it outputs exactly the "GOAL" relation you specify) on a brand-new database I set up from your specifications, but so does your second query.

So it seems like something must be wrong with the setup, not with the SQL itself. What does your CREATE TABLE statement look like for these two tables? Specifically, what are the data types that you are comparing? Are the timePURCHASED columns strings under the hood? If so, do they have spaces in them? Does one table have strings and the other DATETIMEs? And to Terje D.'s point, what precisely is the result you're seeing?

Upvotes: 2

Related Questions