gpt4 book ai didi

sql - mysql 中的排名

转载 作者:行者123 更新时间:2023-11-29 06:23:46 28 4
gpt4 key购买 nike

我读到this article ,我在 oracle 中有代码,但我想将其转换为在 MySQL 上工作。在Oracle中,我使用函数rank,有四列可以合格或不合格,我如何在mysql中使用这个,或者,这是不可能的?

这是代码,我想选择最符合条件的行,每行可以完成4列,我想对其中数据较多的列进行排名。

SELECT vlr,
data
INTO vn_vlr,
vd_data
FROM (SELECT a.*, rank() over (ORDER BY nvl(a.id_categoria, -1) DESC,
nvl(a.id_peso, -1) DESC,
nvl(a.id_faixa, -1) DESC,
nvl(a.sexo, ' ') DESC ) rank
FROM tab_regra_pagamento a
WHERE a.id_competicao = pidcompedticao
AND a.tipo = 'NORMAL'
AND a.data_vencimento > SYSDATE
AND nvl(a.id_categoria, vid_categoria) = vid_categoria
AND nvl(a.id_faixa, vid_faixa) = vid_faixa
AND nvl(a.id_peso, vid_peso) = vid_peso
AND nvl(a.sexo, vsexo) = vsexo)
WHERE rank = 1;

最佳答案

SELECT  @rank := @rank + (@value <> value),
@value := value
FROM (
SELECT @rank := 0,
@value := -1
) vars,
mytable
ORDER BY
value

在您的情况下,当您只需要第一组值的所有副本时:

SELECT  vlr, data
FROM tab_regra_pagamento a
WHERE a.id_competicao = pidcompedticao
AND a.tipo = 'NORMAL'
AND a.data_vencimento > SYSDATE
AND (a.id_cetegoria, a.id_faixa, a.id_peso, a.sexo) =
(
SELECT ai.id_cetegoria, ai.id_faixa, ai.id_peso, ai.sexo
FROM tab_regra_pagamento ai
WHERE ai.id_competicao = pidcompedticao
AND ai.tipo = 'NORMAL'
AND ai.data_vencimento > SYSDATE
ORDER BY
a.id_cetegoria DESC, a.id_faixa DESC, a.id_peso DESC, a.sexo DESC
LIMIT 1
)

关于sql - mysql 中的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1438764/

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