mysql AUTO_INCREMENT基于另一列

How can I auto increment id column based on the 0 to max (int) value on the price column.

Lets assume that price is ASC, I want the id = 1 increment to start at the $0 mark. What would I have to edit for this code ALTER TABLE table AUTO_INCREMENT = 1 ?

id | price | other columns 
1  | 9
2  | 1
3  | 2
4  | 5

into

id | price | other columns
1  | 1
2  | 2
3  | 5
4  | 9

A easy Way ist to create a new table with one column more with autoincrement.

intert into newtable select '',o.* from oldtable o order by o.price asc;

Then they insert all row in the right order from price in the new table.

if you only want to have a row with rownumber, so you can do it like this:

SELECT @nr:=@nr+1 AS id,c.* FROM mytable c, (SELECT @nr:=0) tmp;

the feld x is NOT autoincrement. its only a non uniqe index

SET @nr:=0;
UPDATE newtable n SET n.x = @nr:=@nr+1 ORDER BY n.price ASC;

sorry, you must send 2 Querys. The first to set rhe Variable nr to 0 and then the update statement

SET @nr:=0;
UPDATE your_table_name t
  SET t.id = @nr:=@nr+1
  ORDER BY t.price ASC;