MySQL UPDATE with Incremental value

When you wish to UPDATE a specific column with an incremental value or UPDATE the column with a string and incremental integers, the following methods can be helpful.

Assign a value to the parameter directly as shown below. There is no requirement to DECLARE it, unlike in SQL Server.

UPDATE the column with an incremental value

SELECT @i:=3330;
UPDATE TableName SET ColumnName = @i:=@i+1;

UPDATE the column with a string and an incremental value

SET @i = 0;
UPDATE TableName SET ColumnName = CONCAT(‘StringName_’, @i:=@i+1);

Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s