Garayed.com  

Go Back   Garayed.com > mySQL
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-15-2007, 10:16 AM
Joachim Durchholz
 
Posts: n/a
Default Strange interaction between GROUP BY and EXISTS condition

Hi all,

I have a query with a WHERE EXISTS clause that returns a nonempty result
set (33 records).
When I add a GROUP BY clause, I get an empty result.

Here's the query:

SELECT teams.id AS team_id, users.id AS user_id
FROM teams
JOIN users ON teams.user_id = users.id
WHERE EXISTS (
SELECT * FROM team_members
WHERE team_members.team_id = teams.id
)
GROUP BY users.id

The intention is:
* Select all teams that are not empty (have at least one corresponding
entry in team_members)
* Aggregate over all nonempty teams of every user
(above, I left out a SUM expression from the real-life query)

When I leave out the WHERE EXISTS clause, I get the expected result: one
record per user, with the data for one of his teams (even for those
users who created just empty teams - that's what the WHERE EXISTS is
supposed to filter out).
When I leave out the GROUP BY clause, I get the expected result: all
nonempty teams.
When I have both clauses, I get an empty result.

What's going on?

Even if the WHERE EXISTS is processed after aggregation, I should get at
least some results: most users have members in their teams. However,
that's probably a red herring, since I believe the WHERE clause is
processed before aggregation anyway (is that correct?).

Insights? Comments?

Regards,
Jo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-15-2007, 12:02 PM
Captain Paralytic
 
Posts: n/a
Default Re: Strange interaction between GROUP BY and EXISTS condition

On Nov 15, 11:16 am, Joachim Durchholz <j...@durchholz.org> wrote:
> Hi all,
>
> I have a query with a WHERE EXISTS clause that returns a nonempty result
> set (33 records).
> When I add a GROUP BY clause, I get an empty result.
>
> Here's the query:
>
> SELECT teams.id AS team_id, users.id AS user_id
> FROM teams
> JOIN users ON teams.user_id = users.id
> WHERE EXISTS (
> SELECT * FROM team_members
> WHERE team_members.team_id = teams.id
> )
> GROUP BY users.id
>
> The intention is:
> * Select all teams that are not empty (have at least one corresponding
> entry in team_members)
> * Aggregate over all nonempty teams of every user
> (above, I left out a SUM expression from the real-life query)
>
> When I leave out the WHERE EXISTS clause, I get the expected result: one
> record per user, with the data for one of his teams (even for those
> users who created just empty teams - that's what the WHERE EXISTS is
> supposed to filter out).
> When I leave out the GROUP BY clause, I get the expected result: all
> nonempty teams.
> When I have both clauses, I get an empty result.
>
> What's going on?
>
> Even if the WHERE EXISTS is processed after aggregation, I should get at
> least some results: most users have members in their teams. However,
> that's probably a red herring, since I believe the WHERE clause is
> processed before aggregation anyway (is that correct?).
>
> Insights? Comments?
>
> Regards,
> Jo


Why not use another JOIN?

SELECT
teams.id AS team_id,
users.id AS user_id
FROM teams
JOIN users ON teams.user_id = users.id
JOIN team_members ON team_members.team_id = teams.id
GROUP BY users.id
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:06 PM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.