- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to increment all the rows of a particular column by 1 in a single MySQL query (ID column +1)?
To increment all the rows of a particular ID column by 1, you need to use UPDATE command and update the table. The syntax of the query is as follows. We have also used ORDER BY here
UPDATE yourTableName SET yourIdColumnName=yourIdColumnName+1 ORDER BY yourIdColumnName DESC;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table IdColumnadd1Demo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY - > ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.12 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.18 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.17 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.08 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.10 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.13 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.25 sec) mysql> insert into IdColumnadd1Demo values(); Query OK, 1 row affected (0.40 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from IdColumnadd1Demo;
The following is the output
+----+ | Id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | +----+ 8 rows in set (0.00 sec)
Here is the query to adjust the MySQL ID column+1
mysql> update IdColumnadd1Demo set Id=Id+1 Order By Id DESC; Query OK, 8 rows affected (0.18 sec) Rows matched: 8 Changed: 8 Warnings: 0
Check the table records once again.
The query is as follows
mysql> select *from IdColumnadd1Demo;
The following is the output
+----+ | Id | +----+ | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | +----+ 8 rows in set (0.00 sec)
- Related Articles
- How to find string count of a particular id in a column using a MySQL query?
- How to set all values in a single column MySQL Query?
- How to handle fragmentation of auto increment ID column in MySQL?
- How to update all the entries except a single value in a particular column using MySQL?
- A single MySQL query to combine strings from many rows into a single row and display the corresponding User Id sum in another column?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- MySQL query to increment one of the column values
- How to concatenate all values of a single column in MySQL?
- Make all column names lower case in MySQL with a single query
- How to add column and index in a single MySQL query?
- Update multiple rows in a single column in MySQL?
- How to get values of all rows in a particular column in openpyxl in Python?
- Get the sum of a column in all MySQL rows?
- MySQL query to count rows with a specific column?

Advertisements