Question

I have two tables:

mysql> desc rank ;
+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| id             | int(11)     | NO   | PRI | NULL    | auto_increment |
| score_id       | int(11)     | NO   | MUL | NULL    |                |
| user_id        | int(11)     | NO   | MUL | NULL    |                |
| rank           | int(11)     | NO   | MUL | NULL    |                |
| last_rank      | int(11)     | NO   | MUL | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+

mysql> desc score ;
+----------------+-------------+------+-----+---------+----------------+
| Field          | Type        | Null | Key | Default | Extra          |
+----------------+-------------+------+-----+---------+----------------+
| id             | int(11)     | NO   | PRI | NULL    | auto_increment |
| points         | int(11)     | NO   | MUL | NULL    |                |
+----------------+-------------+------+-----+---------+----------------+

What I need to do is once a day, update the 'rank' of all records. Once a week, update the 'last_rank' of all records.

I can do a select that return a rank for each row:

SET @r = 0 ;

SELECT a.user_id, a.rank, @r:= (@r+1), a.last_rank,
                                   b.points
FROM rank AS a
JOIN score AS b
WHERE a.score_id = b.id
ORDER BY b.points ;

However, I cant find a way to UPDATE the Rank table with this result...

  • How can I do this?
  • Is there a best (fast) way to do this?

MySQL Version:

mysql> SELECT VERSION();

+------------+
| VERSION()  |
+------------+
| 5.1.61-log |
+------------+

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top