Coding⏱️ 2 min read📅 2026-06-03

How to Fix: MySQL ON DUPLICATE KEY UPDATE for multiple rows insert in single query

Insert multiple rows in a single query with MySQL ON DUPLICATE KEY UPDATE.

Quick Answer: Use the INSERT ... ON DUPLICATE KEY UPDATE statement to achieve this, e.g. $sql = "INSERT INTO beautiful (name, age) VALUES ('Helen', 24) ON DUPLICATE KEY UPDATE age = age + 1";

To achieve this, you can use the ON DUPLICATE KEY UPDATE clause in MySQL. This clause allows you to update existing rows and insert new rows in a single query.

💡 How to Use ON DUPLICATE KEY UPDATE for Multiple Rows Insertion

  • Use the ON DUPLICATE KEY UPDATE clause with a comma-separated list of column names and their corresponding values.

🔧 Example Query

Example Query:

  1. INSERT INTO beautiful (name, age) VALUES ('Helen', 24), ('Katrina', 21), ('Samia', 22);
  2. ON DUPLICATE KEY UPDATE name = VALUES(name), age = VALUES(age)

💡 Conclusion

By using the ON DUPLICATE KEY UPDATE clause, you can insert multiple rows in a single query while updating existing rows if the unique key already exists.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions