MySQL Duplicate Key Update: Updating Multiple Fields
Yes, MySQL's 'ON DUPLICATE KEY UPDATE' clause can be used to update multiple fields. When using the INSERT statement, you can specify multiple columns as a unique index. If the inserted record matches an existing record's unique index, the other columns in the existing record will be updated. For example:
INSERT INTO my_table (id, name, email) VALUES (1, 'John', 'john@example.com') ON DUPLICATE KEY UPDATE name='Jane', email='jane@example.com';
In this statement, the 'id' and 'email' columns form a unique index. If a record already exists with 'id' as 1 and 'email' as 'john@example.com', the 'name' column will be updated to 'Jane' and the 'email' column will be updated to 'jane@example.com' in the existing record.
原文地址: https://www.cveoy.top/t/topic/lUHL 著作权归作者所有。请勿转载和采集!