- 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
Implement MySQL INSERT MAX()+1?
You need to use COALESCE() function for this. The syntax is as follows:
INSERT INTO yourTableName(yourColumnName1,yourColumnName2) SELECT 1 + COALESCE((SELECT MAX(yourColumnName1) FROM yourTableName WHERE yourColumnName2=’yourValue’), 0), ’yourValue’;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table InsertMaxPlus1Demo -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (1.27 sec)
Now you can insert some records in the table using insert command. The query is as follows:
mysql> insert into InsertMaxPlus1Demo(Id,Name) values(1,'John'); Query OK, 1 row affected (0.12 sec) mysql> insert into InsertMaxPlus1Demo(Id,Name) values(1,'Mike'); Query OK, 1 row affected (0.21 sec) mysql> insert into InsertMaxPlus1Demo(Id,Name) values(2,'John'); Query OK, 1 row affected (0.12 sec) mysql> insert into InsertMaxPlus1Demo(Id,Name) values(1,'Larry'); Query OK, 1 row affected (0.20 sec) mysql> insert into InsertMaxPlus1Demo(Id,Name) values(3,'John'); Query OK, 1 row affected (0.18 sec) mysql> insert into InsertMaxPlus1Demo(Id,Name) values(2,'David'); Query OK, 1 row affected (0.17 sec)
Display all records from table using select statement. The query is as follows:
mysql> select *from InsertMaxPlus1Demo;
The following is the output:
+------+-------+ | Id | Name | +------+-------+ | 1 | John | | 1 | Mike | | 2 | John | | 1 | Larry | | 3 | John | | 2 | David | +------+-------+ 6 rows in set (0.00 sec)
Here is the query to insert MAX()+1:
mysql> INSERT INTO InsertMaxPlus1Demo (Id, Name) -> SELECT 1 + coalesce((SELECT max(Id) FROM InsertMaxPlus1Demo WHERE Name='John'), 0), 'John'; Query OK, 1 row affected (0.21 sec) Records: 1 Duplicates: 0 Warnings: 0
The above query is checking for John. It has the Id 3 and the record will be inserted with Id 4.
Now check the table record once again using select statement. The query is as follows:
mysql> select *from InsertMaxPlus1Demo;
The following is the output:
+------+-------+ | Id | Name | +------+-------+ | 1 | John | | 1 | Mike | | 2 | John | | 1 | Larry | | 3 | John | | 2 | David | | 4 | John | +------+-------+ 7 rows in set (0.00 sec)
- Related Articles
- Insert MAX(col)+1 into the same MySQL table?
- Implement INSERT … ON DUPLICATE KEY UPDATE in MySQL
- Can we implement nested insert with select in MySQL?
- C++ Program to Implement Max Heap
- How to implement MAX(distinct…) in MySQL and what is the difference without using DISTINCT?
- How to insert NULL into char(1) in MySQL?
- Implement MySQL trigger in the first table to insert records in the second table?
- Find max and second max salary for a MySQL Employee table?
- INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table
- Find max and second max salary for a MySQL Employee table using subquery?
- What is the MySQL VARCHAR max size?
- Sorting max to min value in MySQL
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- MySQL - Insert current date/time?
- Insert sequential number in MySQL?

Advertisements