mysq分组排序查询

按name分组取val最大的值所在行的数据

select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)  (不建议)
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name (不建议)
select * from (select * from tb ORDER BY val desc) temp GROUP BY name ORDER BY val desc;

https://www.cnblogs.com/fps2tao/p/9041175.html

posted @ 2020-11-16 09:14  明月出青山root  阅读(121)  评论(0编辑  收藏  举报