If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > JOIN to get rows in one table that are not in the other

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-02-04, 02:51
MvdM MvdM is offline
Registered User
 
Join Date: Nov 2004
Posts: 5
JOIN to get rows in one table that are not in the other

I've got two identical tables: sw_scan and software.
Now I want to get all rows from sw-scan that don't exist in table software.

With my statement I get all of the rows from table 1 with NULL for all table 2 values..

SELECT *
FROM sw_scan
LEFT JOIN software USING(Manufacturer,Product,Version,Computer)
WHERE software.manufacturer IS NULL;

oh, and (Manufacturer, Product, Version, Computer) is primary key in both tables ..

Last edited by MvdM; 11-02-04 at 02:53.
Reply With Quote
  #2 (permalink)  
Old 11-02-04, 02:59
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
well, that should work, the only thing i can suggests is don't use USING
Code:
SELECT *
  FROM sw_scan 
LEFT 
  JOIN software 
    on sw_scan.Manufacturer 
     = software.Manufacturer
   and sw_scan.Product      
     = software.Product
   and sw_scan.Version      
     = software.Version
   and sw_scan.Computer    
     = software.Computer
 WHERE software.Manufacturer IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-02-04, 03:03
MvdM MvdM is offline
Registered User
 
Join Date: Nov 2004
Posts: 5
hmm.. thanks..
but I already tried lots of things.. to be sure.. and this was one of them...
sth really weird is going on here..
Reply With Quote
  #4 (permalink)  
Old 11-02-04, 03:07
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
well, the table 2 columns should all be null, because that's what you're looking for, right?

by the way, what database is this?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-02-04, 03:13
MvdM MvdM is offline
Registered User
 
Join Date: Nov 2004
Posts: 5
They should be NULL... but the resultset should only contain the rows that have NULL at the software.Manufacturer field after the JOIN.
I used exactly the smae statements to create and fill the tables, only changing the table name, and afterwards I deleted 10 rows from software...
So my resultset should have ten rows. Instead it outputs all 1000 rows filling all fields from table software with NULL.

It's a MySQL 4 db
Reply With Quote
  #6 (permalink)  
Old 11-02-04, 03:15
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
do you get all 990 rows when you change LEFT to INNER?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 11-02-04, 03:24
MvdM MvdM is offline
Registered User
 
Join Date: Nov 2004
Posts: 5
When I make it an INNER JOIN, I get an empty set..
And if I also get rid of the WHERE clause I get the 990 rows...
Reply With Quote
  #8 (permalink)  
Old 11-02-04, 06:27
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
i'm running out of ideas

try REPAIRing the database
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 11-02-04, 10:44
MvdM MvdM is offline
Registered User
 
Join Date: Nov 2004
Posts: 5
Thumbs up

Solved !

After I dropped the tables.. recreated them (with exactly the same statement) and filled them again, it worked..

thnx for the help Rudy!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On