当前位置:Gxlcms > PHP教程 > mysql查寻重复姓名且年龄最大的列表

mysql查寻重复姓名且年龄最大的列表

时间:2021-07-01 10:21:17 帮助过:28人阅读

mysql 查找重复姓名且年龄最大的列表

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)

这里找到count 重复的数据
下面接着找 count 最大,切age 最大且相同的数据

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)

为什么少了一条 dtest ,dtest的数据和ctest在count和age上是一样的?

求指教!谢谢

分享到:


------解决方案--------------------
select tmp1.count,tmp1.age from (select count,max(age) as 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) as tempd,( select count(*) as count ,name,sum(age) as age from t1 group by
name order by count desc ,age desc ) as tmp1 where tmp1.count=tempd.count and tmp1.age=tempd.age count order by tmp1.count desc ,tmp1.age desc;

人气教程排行