To update all the values in a column to John1, John2, etc.; you need to set incremental values 1, 2, 3, etc. and concatenate them to the records. Let us first create a table −
mysql> create table DemoTable ( StudentId varchar(80) ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command. Here, for our example, we have set similar names −
mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | StudentId | +-----------+ | John | | John | | John | | John | | John | +-----------+ 5 rows in set (0.00 sec)
Following is the query to update/concatenate all names with numeric incremental values −
mysql> update DemoTable,(select @row := 0) r set StudentId =concat('John',@row := @row+ 1); Query OK, 5 rows affected (0.11 sec) Rows matched: 5 Changed: 5 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-----------+ | StudentId | +-----------+ | John1 | | John2 | | John3 | | John4 | | John5 | +-----------+ 5 rows in set (0.00 sec)