首页  >  文章  >  后端开发  >  mysql 查寻重复姓名且年龄最大的列表

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

WBOY
WBOY原创
2016-06-13 12:43:59924浏览

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

<br />
mysql> select count(*) as count  ,name,sum(age) as age from t1 group by name order by count desc;<br />
+-------+--------+------+<br />
| count | name   | age  |<br />
+-------+--------+------+<br />
|     3 | atest  |   64 |<br />
|     2 | btest  |   37 |<br />
|     2 | ctest  |   43 |<br />
|     2 | dtest  |   43 |<br />
|     1 | mary   |   22 |<br />
|     1 | kou    |   22 |<br />
|     1 | perter |   23 |<br />
|     1 | kate   |   19 |<br />
+-------+--------+------+<br />
8 rows in set (0.00 sec)<br />

这里找到count 重复的数据
下面接着找 count 最大,切age 最大且相同的数据
<br />
mysql> select count,name,age from ( select count(*) as count  ,name,sum(age) as age from t1 group by<br />
 name order by count desc ,age desc ) as tmp group by count order by count desc ,age desc;<br />
+-------+--------+------+<br />
| count | name   | age  |<br />
+-------+--------+------+<br />
|     3 | atest  |   64 |<br />
|     2 | ctest  |   43 |<br />
|     1 | perter |   23 |<br />
+-------+--------+------+<br />
3 rows in set (0.00 sec)<br />

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

求指教!谢谢

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn