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.
📋 Table of Contents
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 UPDATEclause with a comma-separated list of column names and their corresponding values.
🔧 Example Query
Example Query:
- INSERT INTO beautiful (name, age) VALUES ('Helen', 24), ('Katrina', 21), ('Samia', 22);
- 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.
❓ Frequently Asked Questions
🛠️ Related Fixes
How to Fix: Stuck in tutorial hell after 4 years: How do I b
Fix Stuck in tutorial hell after 4 years: How do I bui. Practice build
How to Fix: Trying to sync mutliple audio tracks to a movie
Fix Trying to sync mutliple audio tracks to a movie bu. Consider using
How to Fix: Failed to merge latest branches from upstream re
Fix Failed to merge latest branches from upstream repo. Try running 'g