SELECT id,substring_index(GROUP_CONCAT(id ORDER BY score desc),',',3) as num from test GROUP BY class