ZBLOG

mysql select count and group by and sum all count

To use the COUNT and GROUP BY functions in MySQL to count the number of rows in a table based on a specific column and then sum all of the counts, you can use the following query:

SELECT COUNT(*), <column>
FROM <table>
GROUP BY <column>
WITH ROLLUP;

The COUNT(*) function will count the number of rows in the table, while the <column> variable specifies the column you want to group by. The GROUP BY function groups the results based on the specified column.

The WITH ROLLUP clause at the end of the query will add an extra row to the result set that shows the total for each group. This row will have a NULL value for the <column> variable.

To sum all of the counts, you can use the SUM function on the COUNT(*) column in a subquery:

SELECT SUM(counts)
FROM (
  SELECT COUNT(*) AS counts, <column>
  FROM <table>
  GROUP BY <column>
) AS counts_table;

This query first uses the COUNT(*) function and the GROUP BY function to count the number of rows for each value in the specified column, and then aliases the resulting count as counts. The outer query then uses the SUM function to add up all of the counts values from the subquery.

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=892

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?