How to get a total count when also grouping in sql?
I am attempting to get a COUNT for a mysql query. Its pretty simple:
SELECT COUNT(id) FROM table WHERE [insert rule here] GROUP BY grouper
The problem is because of that group by at the end, I end up getting a count of rows for each group, when I simply want a total.
Currently to do this I'll put the results in an array from a query that 开发者_高级运维looks more like this:
$res = mysql_query(SELECT * FROM table WHERE [insert rule here] GROUP BY grouper)
$count = mysql_num_rows($res);
This works ok, but I'd just like to use COUNT.
How is this accomplished?
Add WITH ROLLUP
after your group by statement: SELECT COUNT(id) FROM table WHERE [insert rule here] GROUP BY id WITH ROLLUP
at the risk of sounding stupid (or misinterpreting your question):
remove the group by
clause?
精彩评论