MySQL dense_rank 不跳号
MySQL dense_rank not skipping numbers
我是 MYSQL 的新手,我在使密集排名正常工作时遇到问题,我希望有人能够帮助调整我的查询以获得所需的结果。我遇到的问题是在相同的排名之后进入下一个数字而不是跳过一个数字。我在下面显示了当前结果和期望的结果。
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;
当前结果:
期望的结果:
基本上你需要在排序之前对 tdata 进行排序。
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 | 1 | 0 | 2543
probins | 2 | 0 | 2204
rwatson | 3 | 0 | 2010
nbk | 3 | 1 | 2010
tthamos | 3 | 2 | 2010
en,acdonald | 6 | 0 | 1950
dmaginis | 7 | 0 | 1927
db<>fiddle here
对于 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 | 1 | 0 | 2543
probins | 2 | 0 | 2204
tthamos | 3 | 0 | 2010
nbk | 3 | 1 | 2010
rwatson | 3 | 2 | 2010
acdonald | 6 | 0 | 1950
dmaginis | 7 | 0 | 1927
db<>fiddle here
nbk - 很抱歉图片下次会确保使用文字。再次感谢您的宝贵时间。不幸的是,它只有在以正确顺序输入数据时才有效。如果您要将最后三个条目放在第一位,那么 tthomos 将 运行ked #1。
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');
然后 运行:
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
你会得到:
tthamos 1 0 2010
acdonal 2 0 1950
dmaginis 3 0 1927
jkoffa 4 0 2543
probins 5 0 2204
rwatshon 6 0 2010
我是 MYSQL 的新手,我在使密集排名正常工作时遇到问题,我希望有人能够帮助调整我的查询以获得所需的结果。我遇到的问题是在相同的排名之后进入下一个数字而不是跳过一个数字。我在下面显示了当前结果和期望的结果。
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;
当前结果:
期望的结果:
基本上你需要在排序之前对 tdata 进行排序。 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 | 1 | 0 | 2543 probins | 2 | 0 | 2204 rwatson | 3 | 0 | 2010 nbk | 3 | 1 | 2010 tthamos | 3 | 2 | 2010 en,acdonald | 6 | 0 | 1950 dmaginis | 7 | 0 | 1927
db<>fiddle here
对于 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 | 1 | 0 | 2543 probins | 2 | 0 | 2204 tthamos | 3 | 0 | 2010 nbk | 3 | 1 | 2010 rwatson | 3 | 2 | 2010 acdonald | 6 | 0 | 1950 dmaginis | 7 | 0 | 1927
db<>fiddle here
nbk - 很抱歉图片下次会确保使用文字。再次感谢您的宝贵时间。不幸的是,它只有在以正确顺序输入数据时才有效。如果您要将最后三个条目放在第一位,那么 tthomos 将 运行ked #1。
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');
然后 运行:
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
你会得到:
tthamos 1 0 2010
acdonal 2 0 1950
dmaginis 3 0 1927
jkoffa 4 0 2543
probins 5 0 2204
rwatshon 6 0 2010