开发者

How to get a total count when also grouping in sql?

开发者 https://www.devze.com 2023-04-09 09:13 出处:网络
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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号