mysql> select * from dp_ubadge_rules;
+-----+-----+
| id1 | id2 |
+-----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 1 | 2 |
| 1 | 2 |
| 2 | 2 |
+-----+-----+
mysql> select COUNT(DISTINCT(id1)) from dp_ubadge_rules ORDER BY id2;
+----------------------+
| COUNT(DISTINCT(id1)) |
+----------------------+
| 3 |
+----------------------+
mysql> select COUNT(DISTINCT(id1)) from dp_ubadge_rules GROUP BY id1;
+----------------------+
| COUNT(DISTINCT(id1)) |
+----------------------+
| 1 |
| 1 |
| 1 |
+----------------------+
注意ORDER BY和GROUP BY的区别,GROUP BY表示将数据分组,每个分组内的数据作为一个数据集,不同的分组的数据互不影响
mysql> select * From dp_ubadge_rules group by id2;
+-----+-----+
| id1 | id2 |
+-----+-----+
| 1 | 1 |
| 1 | 2 |
+-----+-----+
mysql> select * From dp_ubadge_rules group by id1;
+-----+-----+
| id1 | id2 |
+-----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
+-----+-----+
group by只返回每个group中的第一行数据
mysql> select count(*) From dp_ubadge_rules group by id1;
+----------+
| count(*) |
+----------+
| 3 |
| 2 |
| 1 |
+----------+
mysql> select count(*) From dp_ubadge_rules group by id2;
+----------+
| count(*) |
+----------+
| 3 |
| 3 |
+----------+
配合count()就可以计算每个group中的数据量。
阅读(2499) | 评论(0) | 转发(0) |