Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按列值更新mysql排名按列值分组_Mysql_Rank - Fatal编程技术网

按列值更新mysql排名按列值分组

按列值更新mysql排名按列值分组,mysql,rank,Mysql,Rank,从这些例子开始 一, 二, 它们解释了如何计算按列值分组的秩。在我的情况下,发货 我需要更新“排名”栏的, 选择而不是 | iduser | ship | score | rank | +--------+-------+-------+-------+ | 25 | 1 | 7 | 0 | | 25 | 3 | 21 | 0 | | 25 | 4 | 30 | 0 | | 12 | 9

从这些例子开始

一,

二,

它们解释了如何计算按列值分组的秩。在我的情况下,发货

我需要更新“排名”栏的, 选择而不是

| iduser | ship  | score | rank  |
+--------+-------+-------+-------+
|   25   |   1   |   7   |   0   |
|   25   |   3   |  21   |   0   |
|   25   |   4   |  30   |   0   |
|   12   |   9   |  23   |   0   |
|   25   |   9   |  18   |   0   |
|   21   |   9   |   5   |   0   |
必须更新:

| iduser | ship  | score | rank  |
+--------+-------+-------+-------+
|   25   |   1   |   7   |   1   |
|   25   |   3   |  21   |   1   |
|   25   |   4   |  30   |   1   |
|   12   |   9   |  23   |   1   |
|   25   |   9   |  18   |   2   |
|   21   |   9   |   5   |   3   |
这是选择查询

SELECT  iduser,
        ship,
        score,
        ( 
            CASE ship 
            WHEN @curShip 
            THEN @curRow := @curRow + 1 
            ELSE @curRow := 1 AND @curShip := ship END
        ) AS rank
FROM    ship_stats,
        (SELECT @curRow := 0, @curShip := '') r
ORDER BY ship DESC, score DESC;

那么您在寻找更新声明?对我需要更新:)为什么其中一个用户要为您编写代码。至少你自己试一试。如果您遇到问题,请回来问一个具体的问题。请提供您试图编写的代码的示例。我认为从标题中可以清楚地看出这一点。我做了选择。但是我还没能更新。示例在我创建的模式中。并在选择查询中。
SET @curship = 0,@curRow = 0;
UPDATE ship_stats 
SET rank := 
        (CASE ship 
            WHEN @curShip 
            THEN @curRow := @curRow + 1 
            ELSE @curRow := 1 AND @curShip := ship END
        )
ORDER BY ship DESC, score DESC;