views:

74

answers:

2

I have three MySQL queries I need to combine into a single query using joins. I'm having a lot of trouble figuring out how to do it though. Here are the three queries.

select 
    sum(A.a) 
from A join B 
on A.BID = B.id 
where B.userID=userID

select 
    sum(C.c)
from D left join (C) 
on (D.id=C.DID)
 where D.userID = userID

select sum(E.e) from E where E.userID=userID

I need to somehow combine all of these queries into a single query joined with the user table to give me sum(A.a)+sum(C.C)+sum(E.e) for each user.id. I tried a left join across all tables

user left join (A, B, C, D, E)
on ((A.BID = B.id and B.userID=user.id) 
    or (D.id=C.DID and D.userID = user.id) 
    or (E.userID=user.id))

It was pretty much a shot in the dark and it failed spectacularly. I have no idea how to combine these three queries, anyone give me a hint?

Edit to clarify: I need this query to be a join of the user table with the above queries, because I need to be able to select all user ids ordered by the sum_value. The three previous queries were run individually and parameterized with a single userID.

Futher edit: Tried every variation of union all joined to the user query I could think it, it doesn't work. It comes very close to working with some tweaking, but it still returns incorrect values. I'm not really sure why it is doing that.

+3  A: 

Maybe something like:

select sum(sum_value), user_id from (
select 
    sum(A.a) as sum_value, B.userID as user_id
from A join B 
on A.BID = B.id 
GROUP BY B.userID

UNION ALL

select 
    sum(C.c) as sum_value, D.userID as user_id
from D left join (C) 
on (D.id=C.DID)
GROUP BY D.userID

UNION ALL

select sum(E.e) as sum_value, E.userID as user_id from E
GROUP BY E.userID
)
GROUP BY user_id
codykrieger
This won't work - see edit.
Daniel Bingham
Updated - did it off the top of my head (and it's super late, go easy on me if it doesn't work :), still without a join but that will give you the sum by user ID with one query. You can, of course, throw an ORDER BY at the end to get the results in the order you want them in as well.
codykrieger
@codykrieger No worries, thanks for trying. That's pretty much what I just tried and it returns incorrect values. Not really sure why. One of the values has userID=37, and for each sub-query it shows respectively 230, 8 and 50. But in the some it shows up as 488.
Daniel Bingham
+2  A: 
select 
   ( ( select 
        sum(A.a)
    from 
        A join B 
        on A.BID = B.id 
    where 
        B.userID = user.ID ) + 
    ( select 
          sum(C.c)
      from 
          D left join (C) 
          on (D.id = C.DID)
     where D.userID = user.ID ) +
    ( select
          sum(E.e)
      from 
          E 
      where 
          E.userID = user.ID ) )
from
    user
CooL i3oY
This won't work - see edit.
Daniel Bingham
This certainly work - Updated
CooL i3oY
Bingo! Thanks @CooL i3oY :) Didn't even need a join... was barking completely up the wrong tree.
Daniel Bingham
Only one thing remains, you can write it without join.I believe it s speed increase.
CooL i3oY