ホームページ >バックエンド開発 >PHPチュートリアル >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)
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)
ディスカッションに返信します
mysql> select count(*) as count ,name,sum(age) as age from t1 group by name order by count desc, ae desc ;+-------+--------+------+| count | name | age |+-------+--------+------+| 3 | zx | 64 || 2 | xz | 43 || 2 | john | 43 || 2 | tom | 37 || 1 | perter | 23 || 1 | mary | 22 || 1 | kou | 22 || 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 b name order by count desc ,age desc ) as tmp group by count,age order by count desc ,age desc;+-------+--------+------+| count | name | age |+-------+--------+------+| 3 | zx | 64 || 2 | xz | 43 || 2 | tom | 37 || 1 | perter | 23 || 1 | mary | 22 || 1 | kate | 19 |+-------+--------+------+6 rows in set (0.00 sec)
+-------+--------+------+| count | name | age |+-------+--------+------+| 3 | atest | 64 || 2 | ctest | 43 || 2 | dtest | 43 || 1 | perter | 23 |+-------+--------+------+
select count,name,age from ( select count(*) as count ,name,sum(age) as age from t1 group b
name order by count desc ,age desc ) as tmp group by count, name order by count desc,age desc;
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,name order by count desc ,age desc;+-------+--------+------+| count | name | age |+-------+--------+------+| 3 | zx | 64 || 2 | xz | 43 || 2 | john | 43 || 2 | tom | 37 || 1 | perter | 23 || 1 | kou | 22 || 1 | mary | 22 || 1 | kate | 19 |+-------+--------+------+8 rows in set (0.00 sec)
+-------+--------+------+| count | name | age |+-------+--------+------+| 3 | atest | 64 || 2 | ctest | 43 || 2 | dtest | 43 || 1 | perter | 23 |+-------+--------+------+
この期待される結果は SQL ステートメントに反映できますか?
アドバイスお願いします
あまり効率的ではない気がしますが、できます
(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 tempd.count desc ,tempd.age desc;
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 および tmp1.age=tempd.age count order by tmp1.count desc ,tmp1.age desc;
モデレーターさん、ありがとうございます