Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-08-2012, 12:11 AM   PM User | #1
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 105
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
A Join that exlcudes if data does exist

I have two tables that I want to use a join to exclude data if it exists in the other table. I basically want a list of people from table B that dont exist in table A

If A.account_id exists as B.account_id then dont want it to return in the result set. I have done it as an inclusive query using an INNER JOIN. What I need however is for it to exclude them not include them.

My thoughts are to do a LEFT JOIN and then use PHP to exclude the result sets that have a B.account_id value. This seems awfully expensive if a query can return only the required accounts instead. (there could be thousands!)

A push in the right direction would be greatly appreciated
itxtme is offline   Reply With Quote
Old 09-08-2012, 04:01 AM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
Code:
SELECT
 b.account_id
 larry,
 curly,
 moe
FROM 
 table b
WHERE
 b.account_id
NOT IN
 (SELECT a.account_id FROM table a)
guelphdad is offline   Reply With Quote
Old 09-08-2012, 04:02 AM   PM User | #3
Truffle
New Coder

 
Join Date: Feb 2006
Location: Texas
Posts: 80
Thanks: 1
Thanked 8 Times in 8 Posts
Truffle is an unknown quantity at this point
Code:
SELECT id,name,number FROM goodcustomers

UNION

SELECT id,name,number FROM badcustomers WHERE joinme NOT IN (SELECT id FROM goodcustomers)
Truffle is offline   Reply With Quote
Old 09-08-2012, 05:39 AM   PM User | #4
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 105
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
I love this place, thanks guys. Just when you feel you are starting to gain good developer skills something like this pops up!
itxtme is offline   Reply With Quote
Old 09-10-2012, 12:19 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
> I basically want a list of people from table B that dont exist in table A

Your idea of a left join is right (in addition to Bubikol's method). But you don't need to use PHP at all.

Code:
SELECT B.* 
FROM B LEFT JOIN A ON B.accountid = A.accountid
WHERE A.accountid IS NULL
This will be a bit more efficient than the NOT IN scheme when you have large tables. (If MySQL were a bit smarter, the two would be identical in performance--as they are with SQL Server for example--but it's not. The LEFT JOIN has the advantage that MySQL can optimize the query better especially if both accountid fields are indexed.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 AM.


Advertisement
Log in to turn off these ads.