mysql - How to find top 3 topper of each subject in given table -


id  - name - subject -  marks 1   - acb  - mat    -  90 2   - acb  - sci    -  80 3   - acb  - eng    -  90 4   - acb  -    -  96 5   - acb  - phy    -  70 6   - acb  - che    -  43 7   - xyz  - mat    -  90 8   - xyz  - sci    -  80 9   - xyz  - eng    -  90 10  - xyz  -    -  96 11  - xyz  - phy    -  70 13  - xyz  - che    -  43  etc ..... 

just want show 3 topper of each subject

abc - math - 90 xyz - math - 90 def - math - 80 etc 

you can using variables.

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

Comments

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -