Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update values incrementally in mysql

One field of my table's field is set to 0 for all rows. But I want to update the incremental value by step 1 in an update query.

How can I do that in mysql?

like image 398
hd. Avatar asked Dec 14 '10 12:12

hd.


People also ask

How do I increment in MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

How do I UPDATE multiple values in MySQL?

MySQL UPDATE multiple columnsMySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.

How do I UPDATE values in MySQL workbench?

UPDATE `table_name` is the command that tells MySQL to update the data in a table . SET `column_name` = `new_value' are the names and values of the fields to be affected by the update query. Note, when setting the update values, strings data types must be in single quotes.


2 Answers

Try this:

mysql> select @i := 0; mysql> update bar set c = (select @i := @i + 1); 
like image 66
Pablo Santa Cruz Avatar answered Oct 14 '22 23:10

Pablo Santa Cruz


SET @a = 0;   UPDATE customers SET id = @a:=@a+1; 

You can go for this as well.

like image 38
DRUPWAY Avatar answered Oct 15 '22 01:10

DRUPWAY