开发者

Generating data summaries, row counts for multiple columns in MySQL

开发者 https://www.devze.com 2023-03-30 05:49 出处:网络
I have a registration system. So basically, I have fields like age, gender, etc. This is a simplified schema of my MySQL Database:

I have a registration system. So basically, I have fields like age, gender, etc.

This is a simplified schema of my MySQL Database:

---------------------------------------
| id | name | age | gender | category |
---------------------------------------

Basically I want to generate overall reports like the total number of registrants in a category, total number of males, total number of females, etc.

I have a basic knowledge of SQL. I know that I can use COUNT and GROUP BY to see "summaries" for a single column. So basically, I'd have to do multipl开发者_如何学JAVAe queries to get "summaries" for all columns. Can I do this in one query instead?

I also am going to provide detailed summaries. By this I mean, I'd have to tally how many males are in a category, how many minors are in a category, etc. It's sort of like a category X age, category X gender summary. Again, I know how to do this using multiple queries but I would like some opinion on a more elegant solution.


I think you can accomplish wat you're looking for using WITH ROLLUP:

SELECT id, name, age, gender, category, count(*) cnt
from my_table
GROUP BY id. name, age, gender, category
WITH ROLLUP;
0

精彩评论

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

关注公众号