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 > [newbie] Can I avoid this subquery... and do I need to?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-07-07, 09:03
jmpfffc jmpfffc is offline
Registered User
 
Join Date: Jan 2007
Posts: 12
[newbie] Can I avoid this subquery... and do I need to?

I want to select all users not beloning to a certain usergroup, OR not being in the usergroup-table at all. I have the following query which does exactly that:

($usergroup is the group where I want to see users from that are NOT in it)

Code:
SELECT	*
FROM		`users`
WHERE	`users`.`userid`
NOT IN (SELECT `users`.`userid` FROM `usergroups` WHERE `usergroups`.`groupid` = '".$usergroup."')
I'm pretty new at this and I'm not sure if the above query is the smart way of doing this.
Reply With Quote
  #2 (permalink)  
Old 06-07-07, 11:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT users.*
  FROM users
LEFT OUTER
  JOIN usergroups
    ON usergroups.userid = users.id
   AND usergroups.groupid = $usergroup
 WHERE usergroups.userid IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-08-07, 06:30
jmpfffc jmpfffc is offline
Registered User
 
Join Date: Jan 2007
Posts: 12
Thanks r937. Can you tell me which of the two is the preffered way? Is there any difference at all?
Reply With Quote
  #4 (permalink)  
Old 06-08-07, 06:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if i offer an opinion about a preference, it would only be my personal opinion

best would be for you to test both and decide which you prefer

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-08-07, 07:19
jmpfffc jmpfffc is offline
Registered User
 
Join Date: Jan 2007
Posts: 12
I found no difference in execution time with my current (small) test data, next week I get all the data that is supposed to go in the database, so I will give it a shot then.

But I do value your personal opinion, because in my situation I have no collegues or friends who are busy with this kind of thing, so I can never have a true exchange of ideas, a conversation. I usually read tutorials, but they also reflect the author's opinion. Many times I come across a different tutorial that has a different view, and for now I lack the insight to judge which of the tutorials would be the best.
Reply With Quote
  #6 (permalink)  
Old 06-08-07, 08:16
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
from what I've read on various forums, the outer join is supposed to perform more quickly than the subquery.
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