gpt4 book ai didi

mysql - 如何设置最高值的排名

转载 作者:行者123 更新时间:2023-11-29 17:16:59 27 4
gpt4 key购买 nike

我有一个表名称candidateSalary,例如

+---------------+-----------+
| candidateID | Salary |
+---------------------------+
| 1 | 10000 |
| 2 | 12000 |
| 3 | 8000 |
| 4 | 5000 |
+---------------------------+

我只想写一个查询来了解我的工资排名。我正在尝试 SQL 的 RANK() 和 DENSE_RANK() 函数,但不适用于 mysql。

我想要的输出是

select candidateID,Salary,rank from candidateSalary where candidateID = 2

+---------------+-----------+------+
| candidateID | Salary | rank |
+---------------------------+------+
| 2 | 12000 | 1 |
+---------------------------+------+

如果候选人排名不是 1 或 2,则所有其他候选人的排名应为 3。

最佳答案

这将返回与dense_rank()等效的内容:

select count(distinct cs.salary)
from candidateSalary cs
where cs.salary >= (select cs2.salary
from candidateSalary cs2
where cs2.candidateId = 2
);

或者,如果您希望所有候选人都这样做:

select cs.*,
(select count(distinct cs.salary)
from candidateSalary cs2
where cs2.salary >= c2.salary
) as dense_rank
from candidateSalary cs;

关于mysql - 如何设置最高值的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51518165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com