首页 > 代码库 > MySql按字段分组取最大值记录
MySql按字段分组取最大值记录
要求:获得按table1_id分组,并且age最大的记录信息,即2、3、5条
方法一:
select * from (select * from table2 order by age desc) as a group by a.table1_id
方法二:
select a.* from table2 as a where age = (select max(age) from table2 where a.table1_id=table1_id)
方法三:
select a.* from table2 as a where not exists (select * from table2 where table1_id=a.table1_id and age>a.age)
方法四:
select a.* from table2 as a where exists (select count(*) from table2 where table1_id=a.table1_id and age>a.age having count(*)=0)
MySql按字段分组取最大值记录
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。