时间:2021-07-01 10:21:17 帮助过:28人阅读
mysql> select count(*) as count ,name,sum(age) as age from t1 group by name order by count desc;
+-------+--------+------+
| count | name | age |
+-------+--------+------+
| 3 | atest | 64 |
| 2 | btest | 37 |
| 2 | ctest | 43 |
| 2 | dtest | 43 |
| 1 | mary | 22 |
| 1 | kou | 22 |
| 1 | perter | 23 |
| 1 | kate | 19 |
+-------+--------+------+
8 rows in set (0.00 sec)
mysql> select count,name,age from ( select count(*) as count ,name,sum(age) as age from t1 group by
name order by count desc ,age desc ) as tmp group by count order by count desc ,age desc;
+-------+--------+------+
| count | name | age |
+-------+--------+------+
| 3 | atest | 64 |
| 2 | ctest | 43 |
| 1 | perter | 23 |
+-------+--------+------+
3 rows in set (0.00 sec)