Mysql
 sql >> Database >  >> RDS >> Mysql

Come trovare i primi 3 topper di ogni soggetto nella tabella data

Puoi farlo usando le variabili.

select t.*
from (select t.*,
             (@rn := if(@s = subject, @rn + 1,
                        if(@s := subject, 1, 1)
                       )
             ) as rn
      from t cross join
           (select @rn := 0, @s := '') params
      order by subject, marks desc
     ) t 
where rn <= 3
order by t.subject, t.rn;