MySql按一个字段分组并且取另一个字段最大值的记录
2017-12-22 10:29
447 查看
要求:获得按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根据某一个或者多个字段查找重复数据,并且保留某字段值最大的记录
- mysql group by分组,根据一个字段分组 ,又想得到另一个字段的最大或者最小。如何解决。
- Mysql分组后获取其中一个字段最大值的整条记录
- Mysql按字段分组取最大值记录
- SQL 分组后获取其中一个字段最大值的整条记录
- Mysql按字段分组取最大值记录
- Mysql按字段分组取最大值记录
- Mysql按字段分组取最大值记录
- SQL 分组后获取其中一个字段最大值的整条记录
- MySql按字段分组取最大值记录 [此博文包含图片]
- SQL 分组后获取其中一个字段最大值的整条记录
- Mysql按字段分组取最大值记录
- Mysql按字段分组取最大值记录
- Mysql按字段分组取最大值记录
- SQL 分组后获取其中一个字段最大值的整条记录
- oracle 一个字段做为分组条件,另一个字段的最大值为查询条件
- MySQL查询:查询一个表中类别字段中Max()最大值对应的记录
- SQL 分组后获取其中一个字段最大值的整条记录
- mysql分组查询获取组内某字段最大的记录
- Mysql按字段分组取最大值记录