您可以使用变量来执行此操作。
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 <= 3order by t.subject, t.rn;

您可以使用变量来执行此操作。
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 <= 3order by t.subject, t.rn;