Mysql 获取表中 对某个字段进行分组 然后在分组之后的基础上 再组内排序 获取 分组后最新或者最大记录
文章目录
1、新建User表,结构,及表中数据如下

2、查询User表中,相同年龄最大的一条记录
3、查询语句:
select * from (
select ROW_NUMBER() over(partition by tt.name order by tt.age desc) RowNum
,tt.*
from user tt) as t1 where RowNum = 14、查询结果:
5、查询,每组中,年龄最小的,需要查出所有的最小年龄记录
6、查询语句:
select t1.* from user t1
INNER JOIN
(select * from (
select ROW_NUMBER() over(partition by tt.name order by tt.age ASC) RowNum
,tt.*
from user tt) as t1 where RowNum = 1) t2
on t1.`name` = t2.name and t1.age = t2.age
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论