Jump to content

Using an intermediate table for something that "doesn't" match!


jtakkinen_82

Recommended Posts

Hey, me again...

 

Now I got this problem where I need to to use an intermediate table in order to find something that cannot be found from there... let me explain:

 

I got these tables...

-------

users

-------

user_id (pk)

etc...

 

-------

teams

-------

team_id (pk)

etc...

 

------------

teams_users (to bind those 2 tables together)

------------

user_id (fk)

team_id (fk)

 

Now of course I can find info from the tables using that intermediate table and some known value... BUT, what I'd need to do now is to find all the user_id's from the users table that CAN NOT be found from the intermediate tables (teams_users) user_id column. Is it possible? I got something like this but it obviously doesn't work:

 

SELECT users.user_id, users.username

FROM users JOIN

teams_users ON users.user_id = teams_users.user_id

WHERE teams_users.user_id != users.user_id

 

Come on gurus, I know you can do it if it's possible!  ;)

Link to comment
Share on other sites

How bout this:

 

SELECT user_id, username

FROM users

WHERE user_id NOT IN (

    SELECT DISTINCT(user_id)

    FROM teams_users

)

 

YES! That's got it. How the hell didn't I think of that? Well, that's what you get when you're a n00b with MySQL like me. Thanks again, man!

Link to comment
Share on other sites

Yuck, DISTINCT, rhodesa you're an awful man...

 

How about:

 

SELECT user_id
      ,username
FROM users u
LEFT JOIN teams_users tu ON u.user_id = tu.user_id
WHERE tu.user_id IS NULL
GROUP BY u.user_id, username

 

 

Link to comment
Share on other sites

Yuck, DISTINCT, rhodesa you're an awful man...

 

How about:

 

SELECT user_id
      ,username
FROM users u
LEFT JOIN teams_users tu ON u.user_id = tu.user_id
WHERE tu.user_id IS NULL
GROUP BY u.user_id, username

 

 

 

What's wrong with using DISTINCT? Sorry for asking so many questions but I really would like to know :).

Link to comment
Share on other sites

I read about it a while back, http://www.onlamp.com/pub/a/onlamp/2004/09/30/from_clauses.html which stemmed my thought and confirmed what I suspected for a while. It's an unhealthy function in my opinion and encourages bad practices for those who should be writing correct ANSI JOINs.

 

Ok, well I'll ditch using distinct then and try doing it your way.

Link to comment
Share on other sites

I understand that the JOIN would be better. But would the nested SELECT run faster without the DISTINCT?

 

So is this:

SELECT username FROM users WHERE user_id NOT IN (SELECT user_id FROM teams_users)

better then this?

SELECT username FROM users WHERE user_id NOT IN (SELECT DISTINCT(user_id) FROM teams_users)

Link to comment
Share on other sites

Hard to say... that's a dependent subquery -- only run once for the outer query -- and obviously the DISTINCT would make that subquery take longer.  However, NOT IN() would have to check a longer list without the DISTINCT.  They're both bad.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.