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;
精彩评论