我的 table 是
我想在页面上显示每个部分的最高 3 分,但是如果有人在前 3 分中有相似的分,我想根据分降序显示重复的分。我正在使用以下代码。
SELECT Name, Marks FROM mytable AS t1
INNER JOIN (SELECT DISTINCT(Marks) AS best_marks FROM mytable ORDER BY Marks DESC LIMIT 3) AS t2 ON t1.Marks = t2.best_marks ORDER BY Marks DESC, Name ASC;
所以我的最终输出看起来,
请帮助我...我正在努力寻找解决方案。
最佳答案
这是使用 group_concat()
和 substring_index()
获得前三名的技巧。这个想法是获得列表中的前三名。然后使用 find_in_set()
匹配到该列表:
select name, marks
from mytable t1 join
(select section, substring_index(group_concat(distinct Marks order by Marks desc), ',', 3) as Marks3
from mytable
group by section
) tsum
on t1.section = tsum.section and
find_in_set(t1.Marks, tsum.Marks3) > 0
ORDER BY Marks DESC, Name ASC;
Here是一个 SQLFiddle,说明代码在语法上是正确的。
关于mysql - 显示重复记录的最高 3 分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17848080/