How to get a certain record after 'group by' operation

Silver0427 :

Such as I have a table like :

enter image description here

I want to get the highest student from every class. How can I do?

select * 
from (select * 
      from stu 
      order by height desc) 
group by class_id

This doesn't work. Ty

Akina :
SELECT t1.* 
FROM stu t1
JOIN ( SELECT class_id, MAX(height) height 
       FROM stu 
       GROUP BY class_id ) t2 USING (class_id, height)

If more than one student have the same maximal height over a class then all of them will be returned.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13898&siteId=1
Recommended