计算多个条目/统计数据

时间:2012-10-20 22:44:35

标签: mysql

我想在网站上添加一项调查。一项好的调查需要报告。一些基本报告已经完成。现在我想在咖啡上加点奶油......

包含样本数据的表格:

mysql> select * from u001;
+----+----------+------------+-------+---------------------+
| id | drink    | sex        | age   | date                |
+----+----------+------------+-------+---------------------+
|  1 | Beer     | m          | 30-39 | 2012-10-17 23:17:52 |
|  2 | Milk     | f          | 10-19 | 2012-10-18 00:15:59 |
|  3 | Milk     | f          | 20-29 | 2012-10-18 23:33:07 |
|  4 | Tea      | m          | 30-39 | 2012-10-20 22:47:08 |
|  5 | Water    | f          | 20-29 | 2012-10-20 22:47:30 |
|  6 | Milk     | m          | 30-39 | 2012-10-20 22:51:22 |
+----+----------+------------+-------+---------------------+
6 rows in set (0.00 sec)

我想得到一个结果,计算有多少女性/男性喜欢茶/啤酒等。 这样的理想结果:

+-------+-----+---------+
| drink | sex | counted |
+-------+-----+---------+
| Beer  | m   | 1       |
| Milk  | f   | 2       |
| Tea   | m   | 1       |
| Water | f   | 1       |
| Milk  | m   | 1       |
+-------+-----+---------+

有人提出一些建议或解决方案吗? 提前谢谢。

2 个答案:

答案 0 :(得分:5)

SELECT drink, sex, COUNT(id) counted
FROM   u001
GROUP BY drink, sex

SQLFiddle Demo

答案 1 :(得分:2)

select drink, sex, count(id) from u001 group by drink, sex