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.
如果您只想使用具有rownumber的行,则可以这样做:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.