sql输出作为排名

发布于 2024-09-28 15:41:31 字数 107 浏览 7 评论 0原文

如果我在 sql 表中有一个随机值表,我如何导出并显示它们作为排名而不是绝对值...例如..如果三个值是 30 85 和 90 我如何得到 30显示为 1 或 1st,85 显示为 2 或 2nd 等

If I have a table of random values in a sql table, how can I export and display them as a raking rather than the absolute value... for example.. if the three values are 30 85 and 90 how do i get 30 do display as 1 or 1st, 85 as 2 or 2nd etc

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

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

发布评论

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

评论(2

残龙傲雪 2024-10-05 15:41:31

MySQL 不支持分析函数(ROW_NUMBER、RANK、DENSE_RANK),而您通常会使用分析函数来满足此类要求。

使用:

  SELECT @rownum := @rownum + 1 AS ranking
    FROM YOUR_TABLE t
    JOIN (SELECT @rownum := 0) r
ORDER BY t.value

示例:

  SELECT x.num,
         @rownum := @rownum + 1 AS ranking
    FROM (SELECT 30 AS num
          UNION ALL
          SELECT 85
          UNION ALL
          SELECT 90) x
    JOIN (SELECT @rownum := 0) r
ORDER BY x.num

您可以使用:

SELECT (SELECT COUNT(*)
          FROM YOUR_TABLE b
         WHERE b.value <= a.value) AS ranking
  FROM YOUR_TABLE a

...但重复项将具有相同的排名值。并且你必须确保值比较的方向是正确的。

MySQL doesn't have analytic function support (ROW_NUMBER, RANK, DENSE_RANK), which is generally what you'd use for requirements like these.

Use:

  SELECT @rownum := @rownum + 1 AS ranking
    FROM YOUR_TABLE t
    JOIN (SELECT @rownum := 0) r
ORDER BY t.value

Example:

  SELECT x.num,
         @rownum := @rownum + 1 AS ranking
    FROM (SELECT 30 AS num
          UNION ALL
          SELECT 85
          UNION ALL
          SELECT 90) x
    JOIN (SELECT @rownum := 0) r
ORDER BY x.num

You could use:

SELECT (SELECT COUNT(*)
          FROM YOUR_TABLE b
         WHERE b.value <= a.value) AS ranking
  FROM YOUR_TABLE a

...but duplicates would have the same ranking value. And you have to make sure the value comparison is in the correct direction.

悟红尘 2024-10-05 15:41:31

尝试:

set @i = 0;
select myValue,
        @i:=@i+1 as Ranking
from myTable 
order by myValue ASC

Try:

set @i = 0;
select myValue,
        @i:=@i+1 as Ranking
from myTable 
order by myValue ASC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文