在 MySQL 中生成数据摘要、多列的行计数
我有一个注册系统。基本上,我有诸如年龄、性别等字段。
这是我的 MySQL 数据库的简化架构:
---------------------------------------
| id | name | age | gender | category |
---------------------------------------
基本上我想生成总体报告,例如注册者总数某个类别中的男性总数、女性总数等。
我有 SQL
的基本知识。我知道我可以使用 COUNT
和 GROUP BY
来查看单个列的“摘要”。所以基本上,我必须执行多个查询才能获取所有列的“摘要”。我可以在一个查询中执行此操作吗?
我还将提供详细的总结。我的意思是,我必须计算一个类别中有多少男性,一个类别中有多少未成年人,等等。这有点像类别 X 年龄, 类别 X 性别
摘要。同样,我知道如何使用多个查询来做到这一点,但我想了解一些关于更优雅的解决方案的意见。
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 multiple 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以使用
WITH ROLLUP
来完成您正在寻找的任务:I think you can accomplish wat you're looking for using
WITH ROLLUP
: