从数据库中获取前 5 名排名

发布于 2024-11-09 04:15:18 字数 105 浏览 0 评论 0原文

在我的表格中,我有球队和积分列,我想要获得前 5 名球队。具有相同积分的球队应分组并视为排名之一,因此如果 5 支球队具有相同的积分,则所有球队都应作为排名之一,并且根据球队积分进行下一个后续记录

In my table i have team and points column and I want to get top 5 teams .Teams with same points should be grouped and consider as one of the ranks so if 5 teams are having same points then all should come as one of the rank and next suceeding records according to team points

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沫离伤花 2024-11-16 04:15:18

尝试

SELECT DISTINCT(point), team
FROM tableTeam
ORDER BY points DESC LIMIT 5

TRY

SELECT DISTINCT(point), team
FROM tableTeam
ORDER BY points DESC LIMIT 5
感受沵的脚步 2024-11-16 04:15:18
  SELECT team,
         points,
         (SELECT COUNT(*)
            FROM teams t2
           WHERE t2.points > t1.points) + 1 rank
    FROM teams t1
ORDER BY points DESC
   LIMIT 5
  SELECT team,
         points,
         (SELECT COUNT(*)
            FROM teams t2
           WHERE t2.points > t1.points) + 1 rank
    FROM teams t1
ORDER BY points DESC
   LIMIT 5
始于初秋 2024-11-16 04:15:18

MySQL 中没有窗口函数,因此您需要在脚本中提取排名。

另外,如果我理解您的排名标准,您实际上有兴趣获得前 5 名球队以及可能与排名第 5 的球队拥有相同积分的任何其他球队。

如果是这样,您的限制应该应用于点条件的子查询:

select name, points
from teams
where points >= (
      select points
      from teams
      order by points desc
      limit 1 offset 4
      )
order by points desc, name

如果不是,则使用简单的 order by/limit 即可:

select name, points
from teams
order by points desc, name
limit 5

There's no window functions in MySQL, so you'll want to extract the rank in your scripts.

Also, if I'm making sense of your ranking criteria, you're actually interested in getting the top 5 teams plus any additional teams that might have the same number of points as that in the 5th rank.

If so, your limit should be applied to a subquery on the point criteria:

select name, points
from teams
where points >= (
      select points
      from teams
      order by points desc
      limit 1 offset 4
      )
order by points desc, name

If not, a simple order by/limit will do:

select name, points
from teams
order by points desc, name
limit 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文