如何在mysql中获得前5名

如何在mysql中获得前5名,mysql,sql,database,Mysql,Sql,Database,我试图从学生表中获得前5名的分数。 桌子看起来像那个样 table: student id name score 1 a 100 2 b 100 3 c 90 4 d 87 5 e 85 6 f 88 7 g 83 9 h 92 如果我这样做 select name,score from student ord

我试图从学生表中获得前5名的分数。 桌子看起来像那个样

   table: student

   id name   score

   1    a     100
   2    b     100
   3    c     90
   4    d     87
   5    e     85
   6    f     88
   7    g     83
   9    h     92
如果我这样做

   select name,score from student order by score desc limit 5

   a    100
   b    100
   h     92
   c     90
   f     88
然而,我希望看到这个结果

   a    100
   b    100
   h     92
   c     90
   f     88
   d     87
我正试图找出重复的分数作为第一点 有解决办法吗? 提前感谢。

尝试使用:

这里有一个方法:

select s.*
from student s
where s.score >= (select score
                  from (select distinct score from student order by score desc limit 5) s
                  order by score
                  limit 1
                 )
这将在最内部的子查询中按降序获取分数。它将此限制为五个不同的分数。然后,它找到最小的值,并返回所有具有此分数或更高分数的行。

如下

SELECT s.*
FROM student AS s
  JOIN
    ( SELECT DISTINCT score
      FROM student
      ORDER BY score DESC
          LIMIT 5
    ) AS lim
    ON s.score = lim.score 
ORDER BY s.score DESC ;

你说的“重复分数”是什么意思?你为什么要把e和g排除在你想要的结果之外呢?如果你想找到排名而不是直接计数,这篇文章有一些有趣的技巧来实现这一点:我想你的意思是说“我不能过滤掉重复的分数。”在这种情况下,约翰·康德的答案是给你的。你是在试图数一数得分为100的学生人数吗?还是每个年级得分的学生数?返回五行。示例结果有六行。然后将限制更改为6。他如何知道他是否有更多相同的记录数???…你是说将其限制为6?他们可以将其更改为任何他们想要的。如果他们想要六个,他们可以把它改成六个。如果他们想要更多,他们可以相应地改变。这是基本的SQL。他想说相同的(重复的)记录算作一行,所以我的论点是,不知道有多少行会有相同的记录。我不知道连接也可以这样使用,谢谢
SELECT s.*
FROM student AS s
  JOIN
    ( SELECT DISTINCT score
      FROM student
      ORDER BY score DESC
          LIMIT 5
    ) AS lim
    ON s.score = lim.score 
ORDER BY s.score DESC ;
  SELECT s.id, s.name,s.score FROM tbl_student AS s INNER JOIN (SELECT * FROM tbl_student ORDER BY score DESC LIMIT 0,5) AS s2 ON s2.id=s.id order by s.score desc