简体   繁体   English

MySQL dense_rank 不跳过数字

[英]MySQL dense_rank not skipping numbers

I fairly new to MYSQL and I'm having trouble getting dense rank to work correctly, I was hoping someone would be able to help adjust my query to get the desired results.我对 MYSQL 相当陌生,我无法让密集等级正常工作,我希望有人能够帮助调整我的查询以获得所需的结果。 Issue I'm having is after a ranking that is the same it goes to the next number instead of skipping a number.我遇到的问题是在排名相同之后它会转到下一个数字而不是跳过一个数字。 I have shown both current results and desired results below.我在下面显示了当前结果和预期结果。

        SELECT  gameid,
            score
         , IF(score <> @pscore,@i:=@i+1,@i:=@i) rank
         , @pscore := score          
        FROM playerstats x
        , (SELECT @i:=0,@prev:='',@pscore:='') vars 
        ORDER 
        BY score DESC;

Current Results:当前结果:

当前结果

Desired Results:期望的结果:

期望的结果

Basically you need to sort the tdata before you rank it.基本上,您需要先对 tdata 进行排序,然后再对其进行排名。 MySQL MySQL

 CREATE TABLE playerstats ( `Score` INTEGER, `Player` VARCHAR(11) ); INSERT INTO playerstats (`Score`, `Player`) VALUES ('2543', 'jkoffa'), ('2204', 'probins'), ('2010', 'rwatson'), ('2010', 'nbk'), ('2010', 'tthamos'), ('1950', 'en,acdonald'), ('1927', 'dmaginis');
 SELECT `Player`,IF(@score <> `Score`, @rn:= @rn + @cor +1, @rn:= @rn) rnknumber,IF(@score = `Score`, IF(@cor > 0,@cor:= @cor +1,@cor:= 1), @cor:= 0) correction,@score:= `Score` 'Score' FROM (SELECT `Score`, `Player` FROM playerstats ORDER BY Score DESC) t1, (SELECT @score:= -1) t2,(SELECT @rn:= 0) t3,(SELECT @cor:= 0) t4
 Player |播放器 | rnknumber |号码 | correction |修正 | Score:---------- |:-------- |分数:--------- |:-------- | ---------: | ---------: | ----: jkoffa | ----: jkoffa | 1 | 1 | 0 | 0 | 2543 probins | 2543 个探针 | 2 | 2 | 0 | 0 | 2204 rwatson |第2204章3 | 3 | 0 | 0 | 2010 nbk | 2010 nbk | 3 | 3 | 1 | 1 | 2010 tthamos | 2010 年 | 3 | 3 | 2 | 2 | 2010 en,acdonald | 2010 年,阿克唐纳德 | 6 | 6 | 0 | 0 | 1950 dmaginis | 1950 年7 | 7 | 0 | 0 | 1927 1927年

db<>fiddle here db<> 在这里摆弄

for Mariadb 10.3对于Mariadb 10.3

 CREATE TABLE playerstats ( `Score` INTEGER, `Player` VARCHAR(11) ); INSERT INTO playerstats (`Score`, `Player`) VALUES ('2010', 'tthamos'), ('1950', 'acdonald'), ('1927', 'dmaginis'), ('2010', 'nbk'), ('2543', 'jkoffa'), ('2204', 'probins'), ('2010', 'rwatson');
 SELECT `Player`,IF(@score <> `Score`, @rn:= @rn + @cor +1, @rn:= @rn ) rnknumber,IF(@score = `Score`, IF(@cor > 0,@cor:= @cor +1,@cor:= 1), @cor:= 0) correction,@score:= `Score` 'Score' FROM (SELECT `Score`, `Player` FROM playerstats ORDER BY Score DESC LIMIT 18446744073709551615) t1, (SELECT @score:= -1) t2,(SELECT @rn:= 0) t3,(SELECT @cor:= 0) t4
 Player |播放器 | rnknumber |号码 | correction |修正 | Score:------- |:-------- |分数:------- |:-------- | ---------: | ---------: | ----: jkoffa | ----: jkoffa | 1 | 1 | 0 | 0 | 2543 probins | 2543 个探针 | 2 | 2 | 0 | 0 | 2204 tthamos |第2204章3 | 3 | 0 | 0 | 2010 nbk | 2010 nbk | 3 | 3 | 1 | 1 | 2010 rwatson | 2010 华生 | 3 | 3 | 2 | 2 | 2010 acdonald | 2010 阿克唐纳 | 6 | 6 | 0 | 0 | 1950 dmaginis | 1950 年7 | 7 | 0 | 0 | 1927 1927年

db<>fiddle here db<> 在这里摆弄

nbk - I apologize for the pictures will make sure to use text next time. nbk - 我为图片道歉,下次确保使用文字。 Thank you again for your time.再次感谢您的宝贵时间。 Unfortunately it only works if data is entered in the correct order.不幸的是,它仅在以正确的顺序输入数据时才有效。 If you were to take the bottom three entries and put them in first then tthomos would be ranked #1.如果您要取最后三个条目并将它们放在第一位,那么 tthomos 将排名第一。

CREATE TABLE playerstats  (
  `Score` INTEGER,
  `Player` VARCHAR(11)
);

INSERT INTO playerstats 
  (`Score`, `Player`)
VALUES
  ('2010', 'tthamos'),
  ('1950', 'acdonald'),
  ('1927', 'dmaginis'),
  ('2543', 'jkoffa'),
  ('2204', 'probins'),
  ('2010', 'rwatson');

then ran:然后跑:

SELECT
`Player`
,IF(@score <> `Score` , @rn := @rn + @cor +1, @rn:= @rn) rnknumber
,IF(@score = `Score` , @cor := 1, @cor := 0) correction
,@score := `Score` 'Score'
FROM
(SELECT `Score`, `Player` FROM playerstats ORDER BY Score DESC) t1
, (SELECT @score := -1) t2
,(SELECT @rn := 0) t3,(SELECT @cor := 0) t4

You would get:你会得到:

tthamos   1     0   2010
acdonal   2     0   1950
dmaginis  3     0   1927
jkoffa    4     0   2543
probins   5     0   2204
rwatshon  6     0   2010

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM