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 > Many-to-Many Query Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-09, 16:08
socca socca is offline
Registered User
 
Join Date: Jan 2009
Posts: 16
Many-to-Many Query Help

I have a classical many-to-many DB design: Contractors_table, Trades_table, Contract_Trades.

I am looking to run a query that will return what trades this contractor does not do.

Any help will be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 02-06-09, 18:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
"trades this contractor does not do" --
Code:
SELECT t.id
     , t.name
  FROM Trades_table AS t
LEFT OUTER
  JOIN Contract_Trades AS ct
    ON ct.trade_id = t.id
   AND ct.contractor_id = 937
 WHERE ct.contractor_id IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-07-09, 03:33
bklr bklr is offline
Registered User
 
Join Date: Dec 2008
Posts: 133
hi socca,
if u have small data then try with not in r not exists also queries
according to performance exists will have more performance than in
i think so............
Reply With Quote
  #4 (permalink)  
Old 02-09-09, 08:43
socca socca is offline
Registered User
 
Join Date: Jan 2009
Posts: 16
Thank you r937 that worked perfectly.
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