jaymc Posted May 28, 2008 Share Posted May 28, 2008 This works SELECT count(*) FROM arcade_games GROUP BY champion When I add a SUM in, for another table using a join, COUNT displays the wrong result (the correct result x amount of row matches in arcade_games) SELECT count(*), SUM(arcade_games.plays) FROM arcade_games LEFT JOIN arcade_plays ON arcade_games.champion = arcade_plays.player GROUP BY champion Any ideas? Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/ Share on other sites More sharing options...
amites Posted May 29, 2008 Share Posted May 29, 2008 try assigning count to an individual field, otherwise no telling what it's going to be counting Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-552291 Share on other sites More sharing options...
jaymc Posted May 29, 2008 Author Share Posted May 29, 2008 I had that originally, that did not work either Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-552387 Share on other sites More sharing options...
fenway Posted May 30, 2008 Share Posted May 30, 2008 Did you try count(arcade_games.champion)? Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-553564 Share on other sites More sharing options...
jaymc Posted May 30, 2008 Author Share Posted May 30, 2008 Did you try count(arcade_games.champion)? Yes, it really does not seem to like it Perhaps its an illegal operation? I tried to do something pretty similar a while back and you guys said it was an illegal operation Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-553679 Share on other sites More sharing options...
fenway Posted May 30, 2008 Share Posted May 30, 2008 Sorry, I just re-read the query... COUNT() counts every _matching_ row it encounters in _any_ table, hence MxN. How about this? (very untested) SELECT count(*), SUM (sub.plays) FROM ( SELECT champion, arcade_games.plays FROM arcade_games LEFT JOIN arcade_plays ON arcade_games.champion = arcade_plays.player ) AS sub GROUP BY sub.champion Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-553811 Share on other sites More sharing options...
Barand Posted May 31, 2008 Share Posted May 31, 2008 SELECT count(DISTINCT arcade_games.champion), SUM(arcade_games.plays) FROM arcade_games LEFT JOIN arcade_plays ON arcade_games.champion = arcade_plays.player GROUP BY champion Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-553973 Share on other sites More sharing options...
jaymc Posted May 31, 2008 Author Share Posted May 31, 2008 Great, thanks I actually ended up using a cron job to generate the results and store them in a cache table, which I suppose is better for load However, the syntax above will come in handy for future queries!! Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-554353 Share on other sites More sharing options...
fenway Posted June 2, 2008 Share Posted June 2, 2008 SELECT count(DISTINCT arcade_games.champion), SUM(arcade_games.plays) FROM arcade_games LEFT JOIN arcade_plays ON arcade_games.champion = arcade_plays.player GROUP BY champion Interesting use of DISTINCT... never thought of that. Link to comment https://forums.phpfreaks.com/topic/107711-solved-sum-and-count-2-tables/#findComment-555505 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.