Mysql searches for the list with duplicate names and the largest age. mysql & gt; & nbsp; select & nbsp; count (*) & nbsp; as & nbsp; count & nbsp ;, name, sum (age) & nbsp; as & nbsp; age & nbsp; from & nbsp mysql searches for the list of duplicate names with the largest age
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)
Duplicate count data is found here.
Next, find the largest count, and split the data with the largest age and the same size.
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)
Why is one dtest missing? the data of dtest is the same as that of ctest on count and age?
Please advise! Thank you for sharing :? Select? Count (*)? As? Count ??, Name, sum (age )? As? Age? From? T1? Group? By? Name? Order? By? C... 'data-pics = ''>
------ Solution --------------------
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
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
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;