Mysql按字段分组取最大值记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql按字段分组取最大值记录相关的知识,希望对你有一定的参考价值。
select a.* from table2 as a where age = (select max(age) from table2 where a.table1_id=table1_id)sql语句解答一下,重要讲解后面的where条件 a.table1_id=table1_id
参考技术A 其实这个方法有问题应该这样写select table1_id,max(age) age from table2 group by table1_id
你的where条件 a.table1_id=table1_id是判断当前table1_id的值age是不是最大的本回答被提问者和网友采纳
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按字段分组取最大值记录的主要内容,如果未能解决你的问题,请参考以下文章