Let’s say I have a table named members with this structure:
member_id, col1, col2, number
Now, if I run this query:
SELECT @rownum:=@rownum+1 number, member_id
FROM members, (SELECT @rownum:=0) r ORDER BY col1 DESC, col2 DESC
I get a number for each row. Is it possible to assign this number to column number for each row?
For example, if the table contained this data:
1 1000 1.2 0
2 8700 1.1 0
3 1000 1.1 0
First row number column should be set to 2, second row to 1 and third row to 3.
You may use a temporary table to store the values and
UPDATEfrom it with a join.Actually by my test just now I was able to do this without the temporary table. I didn’t think MySQL would permit updating the same table from a subquery but apparently it is permitted with the correlated subquery.
SQLfiddle.com demo