3

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

带有样本数据的表格:

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       |
+-------+-----+---------+

有人有什么建议或解决方案吗?提前致谢。

4

2 回答 2

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

SQLFiddle 演示

于 2012-10-20T22:45:57.510 回答
2
select drink, sex, count(id) from u001 group by drink, sex
于 2012-10-20T22:46:38.443 回答