我有一个餐桌学生。我需要每个组的前3个最高记录。该查询应适用于动态添加的组。在查询中未设置组的默认值。

学生桌:

id    name    mark       subject
--------------------------------------
1     kannan     60         French
2     balan      77         French
3     raja       88         French
4     sheik      78         French
5     satheesh   98         French
6     Ravi       90         French
7     Vishnu     90         English
8     siva       100        English
9     suresh     50         English
10    ramesh     59         English
11    ganesh     97         English
12    david      58         English


预期结果:

name      mark    subject
---------------------------
Ravi      90      French
Satheesh  98      French
raja      88      French
siva      100     English
ganesh    97      English
ramesh    59      English


我试过的查询:

SELECT name, mark, subject from (SELECT name, mark, subject order by mark
desc LIMIT 3) as ss group by subject, mark;


但是我没有得到正确的值。

最佳答案

(SELECT NAME ,mark, SUBJECT FROM `student ` WHERE SUBJECT = 'French' ORDER BY  mark DESC LIMIT 3)
UNION
(SELECT NAME ,mark, SUBJECT FROM `student ` WHERE SUBJECT = 'English' ORDER BY  mark DESC LIMIT 3)

关于mysql - 从每个组mysql获取前3个最高记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48207300/

10-11 07:18