分组查询 每组最高的某条记录

需求是 查询每个班 年龄最小的那个人 ,需要显示如下:

SQL 语句如下:

select id,
SUBSTRING_INDEX(GROUP_CONCAT(age order by age),‘,‘,1) as age,
SUBSTRING_INDEX(GROUP_CONCAT(username order by age),‘,‘,1) as userName
from person
GROUP BY id

原文地址:https://www.cnblogs.com/refuge/p/9061383.html

时间: 2024-09-30 18:14:32

分组查询 每组最高的某条记录的相关文章

MySQL分组查询每组最新的一条数据

开发中经常会遇到,分组查询最新数据的问题,比如下面这张表(查询每个地址最新的一条记录): sql如下: -- ---------------------------- -- Table structure for test -- ---------------------------- DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10

获取分组后取某字段最大一条记录

获取分组后取某字段最大一条记录 方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from test as b where a.type = b.type );

mysql查询各种类型的前N条记录

mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可  (select * from event_info where event_type = 1  limit 3)union all(select * from event_info where event_type = 2  limit 3)union all(select * from event_info where event_type = 3  limit 3) 原文出处:http://my.oschina.net/u/

获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)

获取分组后取某字段最大一条记录方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from test as b where a.type = b.type ); 方法二:(效率次之) select a.* from test a, (select type,max(typeindex) typeindex from test group by type) b where a.type = b.

MySQL取每组的前N条记录

一.对分组的记录取前N条记录:例子:取前 2条最大(小)的记录 1 1.用子查询: 2 SELECT * FROM right2 a WHERE 2> 3 (SELECT COUNT(*) FROM right2 b WHERE b.id=a.id AND b.account>a.account) 4 ORDER BY a.id,a.account DESC 5 2.用exists半连接: 6 SELECT * FROM right2 a WHERE EXISTS 7 (SELECT COUN

MYSQL-如何查询/修改最大日期的那条记录

参考资料:http://stackoverflow.com/questions/6898935/sql-update-query-with-group-by-clause -- 更新数据 UPDATE product_info AS t INNER JOIN (SELECT product_id,max(update_date) update_date FROM product_info WHERE product_id = 830 GROUP BY product_id) t1 ON t.pr

SQL分组查询每组前几条数据

/*第一种实现方法,效率低并且有错误*/ DECLARE @DD DATETIME SET @DD = GETDATE() SELECT a.GoodsID , a.Account , a.LastUpdate FROM dbo.tb_App_Goods a LEFT JOIN dbo.tb_App_Goods b ON a.Account = b.Account AND a.LastUpdate > b.LastUpdate GROUP BY a.GoodsID , a.Account , a

mysql分组查询获取组内某字段最大的记录

id sid cid 1 1 12 1 23 2 1 以sid分组,最后取cid最大的那一条,以上要取第2.3条 1 方法一: 2 select * from (select * from table order by cid desc) as a group by a.sid 3 4 方法二: 5 select a.* from table as a where cid = (select max(cid) from table where a.sid = sid) 6 7 方法三: 8 se

ORACLE每组只保留一条记录

删除同一组内其他记录 DELETE from memactivities a where exists(select 1 FROM (select Uuid,ci_no,lst_upd_ts,ROW_NUMBER() OVER(PARTITION BY Uuid order by ci_no) rn from ics.memactivities where uuid is not null)b where a.Uuid=b.Uuid AND a.lst_upd_ts=b.lst_upd_ts a