In a mysql shell:

# mysql
SET @a = 0;  
UPDATE topic SET rank = @a:=@a+1;

SELECT rank form topic;
+------+
| rank |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
|    6 |
|    7 |
|    8 |
|    9 |
|   10 |
+------+

MySQL has concept of session variables.

They are loosely typed variables that may be initialized somewhere in a session and keep their value until the session ends.

They are prepended with an @ sign, like this: @var

You can initialize this variable with a SET statement or inside in a query:

# mysql
SET @var := 1
SELECT @var2 := 2

Created on 23-08-2008 by Eric le bihen

comments powered by Disqus