- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to update all the values in a column with numeric incremental values like John1, John2, John3, etc.
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)
- Related Articles
- MySQL query to remove string from a column with values EMP1, EMP2, EMP3, etc.
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
- How to set all values in a single column MySQL Query?
- MySQL query to get the max value with numeric values in varchar field?
- Update all varchar column rows to display values before slash in MySQL?
- MySQL query to get the character length for all the values in a column?
- MySQL query to display record with maximum count values in a group with other column values?
- MySQL update column to NULL for blank values
- MySQL UPDATE column names and set None values with N/A?
- MySQL query to retrieve only the column values with special characters?
- MySQL query to display only the column values with corresponding column having whitespace
- Use LIKE % to fetch multiple values in a single MySQL query
- MySQL query to find the average of only first three values from a column with five values
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Update a column in MySQL and remove the trailing underscore values

Advertisements