【发布时间】:2014-06-17 07:17:11
【问题描述】:
我在制定 MySQL 语句/查询字符串时遇到问题。我有一张桌子(请参阅下面的示例)
这是一个包含性别、年龄和report_requested 字段的示例表。我想要的输出是这样的:
20 years old, male | Total number of users | Total who requested min 1 report | total who requested 2 reports | total 3 or more reports
20 years old female | Total number of users | Total who requested min 1 report | total who requested 2 reports | total 3 or more reports
20 years old combined | Total number of users | Total who requested min 1 report | total who requested 2 reports | total 3 or more reports
21 years old, male | Total number of users | Total who requested min 1 report | total who requested 2 reports | total 3 or more reports
21 years old female | Total number of users | Total who requested min 1 report | total who requested 2 reports | total 3 or more reports
21 years old combined | Total number of users | Total who requested min 1 report | total who requested 2 reports | total 3 or more reports
...等等
但我很难接受。我只知道根据性别和年龄确定请求(1,2,3,...等)信用报告的用户数量。
这是我使用的:
SELECT COUNT(*) as cnt, report_requested
FROM sampletable WHERE age = '39'
AND gender = 'M' GROUP BY report_requested
结果如下:
它只返回 20 岁男性的数据,请求 1 个信用报告的用户数量,2 个信用报告最多 8 个(但这也是错误的,因为它应该结合请求 3 个或更多信用报告的用户数量)
这里的任何人都可以帮助我或告诉我如何完成此任务吗?
【问题讨论】: