首頁  >  文章  >  後端開發  >  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