mysql分組取每組前幾條記錄(排名) 附group by與order by的研究
阿新 • • 發佈:2018-12-11
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name--方法2:select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)--方法3: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--方法4: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 a.name--方法5select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name/*name val memo ---------- ----------- --------------------a 3 a3:a的第三個值b 5 b5b5b5b5b5