MySQL update statement to store ranking positions

后端 未结 5 2124
死守一世寂寞
死守一世寂寞 2020-12-05 03:41

I\'m trying to get my head around a query and I just can\'t figure it out. I would appreciate if someone give me a pointer. As a simple example of what I\'m trying to achiev

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 04:16

    This creates an inline update statement that will rank your players incrementing by the variable @rc. I've used it many times in very similar cases, it works well and keeps it all on the DB side.

    SET @rc = 0;
    UPDATE players JOIN (SELECT @rc := @rc + 1 AS rank, id FROM players ORDER BY rank DESC)
    AS order USING(id) SET players.rank = order.rank;
    

    id is assumed to be the primary key for your players table.

提交回复
热议问题