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 > Database Server Software > MySQL > SQL Exclude question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-16-04, 13:58
pokermagic pokermagic is offline
Registered User
 
Join Date: Oct 2003
Location: santa clara
Posts: 25
SQL Exclude question

i have a sql question... i have two tables one a model table. with modelid's and also a exclude table which also has modelid's. I want to create a query where i want to only show the modelid that do not match what is in the model table. How would i write that?
Reply With Quote
  #2 (permalink)  
Old 09-16-04, 14:05
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
I'd use:
Code:
SELECT m.*
   FROM model AS m
   LEFT OUTER JOIN exclude AS e
      ON (e.modelid = m.modelid)
   WHERE  e.modelid IS NULL
-PatP
Reply With Quote
  #3 (permalink)  
Old 09-16-04, 14:09
RBARAER RBARAER is offline
Registered User
 
Join Date: Aug 2004
Location: France
Posts: 754
Hello,

From what I understand, you want to show modelids from table "exclude" that don't match any modelid from table "model".

Try this :

select exclude.modelid from exclude where exclude.modelid not in (select model_id from model);

Regards,

RBARAER
Reply With Quote
  #4 (permalink)  
Old 09-16-04, 15:29
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
If you are using MySQL 4.1 or newer, I'd recommend using the EXISTS test instead of IN because it is usually much more efficient.

The outer join syntax that I originally posted works on all versions of MySQL.

-PatP
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