1

I want to select data from multiple tables and I want to get the sum from one column in one statement. If I just use sum (column) I limit my results to one row, if I use group by, sum (column) doesnt seem to work. Or I am just too stupid.

There is probably a more elegant solution, too. (its the sum in line 6).

SELECT 
    lb.date AS l_date, 
    gbb.id AS bid, 
    gbb.cash AS b_cash, 
    gpr.cash AS p_cash, 
    sum (gbb.cash) as t_cash,
    gbb.name
FROM 
    log_buildings lb, 
    game_building_basic gbb, 
    game_player_buildings gpb, 
    game_player_ress gpr
WHERE 
    lb.pid = :pid
    AND gpr.id = :pid
    AND gpb.pid = :pid
    AND lb.date >= gpr.date
    AND lb.bid = gbb.id
    AND gpb.buildings_id = gbb.id
    AND gpb.pid = :pid";
6
  • What data items do you want to sum. Sum works by summing together multiple rows. You select which rows it sums by using group.
    – CrazyCasta
    Sep 29, 2012 at 19:43
  • If i put in 'Group by gbb.cash' i only get the row values in t_cash, not summed up. or am i doing it wrong? Sep 29, 2012 at 19:48
  • You need to tell us what group of data you want summed. Do you want to sum all the rows that match your where statement?
    – CrazyCasta
    Sep 29, 2012 at 19:50
  • yes, i want so sum all the rows in gbb.cash that matches the where statement. Sep 29, 2012 at 19:53
  • Then the easiest way to do that would be to get rid of all the other select stuff. If you also want all the other rows that get you that sum then you should use a second query.
    – CrazyCasta
    Sep 29, 2012 at 19:56

1 Answer 1

1

First, you should learn JOIN syntax for your query:

SELECT lb.date AS l_date, gbb.id AS bid, gbb.cash AS b_cash, gpr.cash AS p_cash,
       sum (gbb.cash) as t_cash, gbb.name
FROM log_buildings lb join
     game_building_basic gbb
     on lb.bid = bgg.bid join
     game_player_buildings gpb
     on gpb.buildings_id = gbb.id join
     game_player_ress gpr
     on lb.date >= gpr.date
WHERE lb.pid = :pid AND gpr.id = :pid AND gpb.pid = :pid AND gpb.pid = :pid"

As written, this query will return one set of random data for all the fields, along with the total sum. You can get the total on a separate row, by using rollup:

SELECT lb.date AS l_date, gbb.id AS bid, gbb.cash AS b_cash, gpr.cash AS p_cash,
       sum (gbb.cash) as t_cash, gbb.name
FROM log_buildings lb join
     game_building_basic gbb
     on lb.bid = bgg.bid join
     game_player_buildings gpb
     on gpb.buildings_id = gbb.id join
     game_player_ress gpr
     on lb.date >= gpr.date
WHERE lb.pid = :pid AND gpr.id = :pid AND gpb.pid = :pid AND gpb.pid = :pid"
group by lb.date, gbb.id, gbb.cash, gpr.cash, gbb.name with rollup

However, you want it as a separate column, not a separate row (and rollup produces multiple sums at different levels. For this, you need a subquery:

SELECT lb.date AS l_date, gbb.id AS bid, gbb.cash AS b_cash, gpr.cash AS p_cash,
       gbb.name,
       (select sum(gbb.cash)
        FROM log_buildings lb join
             game_building_basic gbb
             on lb.bid = bgg.bid join
             game_player_buildings gpb
             on gpb.buildings_id = gbb.id join
             game_player_ress gpr
             on lb.date >= gpr.date
        WHERE lb.pid = :pid AND gpr.id = :pid AND gpb.pid = :pid AND gpb.pid = :pid
       ) as t_cash
FROM log_buildings lb join
     game_building_basic gbb
     on lb.bid = bgg.bid join
     game_player_buildings gpb
     on gpb.buildings_id = gbb.id join
     game_player_ress gpr
     on lb.date >= gpr.date
WHERE lb.pid = :pid AND gpr.id = :pid AND gpb.pid = :pid AND gpb.pid = :pid

In almost any other database, you could do this with windows functions:

SELECT lb.date AS l_date, gbb.id AS bid, gbb.cash AS b_cash, gpr.cash AS p_cash,
       gbb.name,sum(gbb.cash) over ()

But MySQL does not support that syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.