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? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
fenway Posted May 30, 2008 Share Posted May 30, 2008 Did you try count(arcade_games.champion)? Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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!! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.