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 > umm.. a sort of anti-join?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-12-09, 09:40
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
umm.. a sort of anti-join?

Another dumb question....
Code:
SELECT e.eventName
FROM events e
INNER JOIN eventCharities ec ON e.idEvents = ec.idEvents
INNER JOIN charities c ON ec.idCharities = c.idCharities
INNER JOIN userCharities uc ON uc.idCharities = c.idCharities
WHERE uc.idUsers = 42
That will get me all events that are associated with one of the user's favourite charities.

However, I also have a table "userEvents" (guess what that does) - I'd like to amend the query above so that it DOESN'T include the events that the user is already taking part in.

The only way I can think of is to plonk a subquery on the bottom. This seems a bit... clunky. Is there a better way?

Code:
SELECT e.eventName
FROM events e
INNER JOIN eventCharities ec ON e.idEvents = ec.idEvents
INNER JOIN charities c ON ec.idCharities = c.idCharities
INNER JOIN userCharities uc ON uc.idCharities = c.idCharities
WHERE uc.idUsers = 42
AND events.idEvents NOT IN (SELECT idEvents FROM userEvents WHERE idUsers = 42)
Thanks
Reply With Quote
  #2 (permalink)  
Old 05-12-09, 10:13
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
It would be faster if you used "not exists" rather than "not in" :
Code:
SELECT e.eventName
FROM events e
INNER JOIN eventCharities ec ON e.idEvents = ec.idEvents
INNER JOIN charities c ON ec.idCharities = c.idCharities
INNER JOIN userCharities uc ON uc.idCharities = c.idCharities
WHERE uc.idUsers = 42
AND NOT EXISTS (
     SELECT 1 FROM userEvents ue 
     WHERE  ue.idUsers = uc.idUsers 
            and ue.idEvents = events.idEvents )
doesn't make it any prettier though.

Mike

PS It's probably better to supply a simple spec in words rather than SQL as readers 1st have to decode your SQL and then try and imagine it done in a different way rather than the way they've just seen it written - which is difficult.
Reply With Quote
  #3 (permalink)  
Old 05-12-09, 10:41
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Cheers - yes, I should have realised that EXISTS would have been faster. And advice duly noted.
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