Нет необходимости хранить вычисляемый столбец position
в таблице. Следующие работы для всех версий:
create table tab ( id int, name varchar(1), points int );
insert into tab values
(1,'a', 0),
(2,'b',120),
(3,'c',130),
(4,'d',120);
select t.id, t.name, t.points,
( case when points = 0 then 'Unranked' else t.rnk end ) as position
from
(
select t1.*,
@rnk := if(@pnt = points,@rnk,@rnk + 1) rnk,
@pnt := points
from tab t1
cross join (select @rnk := 0, @pnt := 0 ) t2
order by points desc
) t
order by t.id;
id name points position
-- ---- ------ --------
1 a 0 Unranked
2 b 120 2
3 c 130 1
4 d 120 2
Если вы хотите сохранить столбец position
в своей таблице, вы можете использовать следующий оператор update
, связывая его через основной столбец id
:
update tab tt
set position = ( select
( case when points = 0 then 'Unranked' else t.rnk end ) as position
from
(
select t1.*,
@rnk := if(@pnt = points,@rnk,@rnk + 1) rnk,
@pnt := points
from tab t1
cross join (select @rnk := 0, @pnt := 0 ) t2
order by points desc
) t
where t.id = tt.id );
Rextester Demo