PHP/MySQL: Update if Exist or Insert if Not
You seek to update a MySQL table if specific fields exist, or insert if they do not. This scenario involves joining three tables: set_colors, school_art, and baseimage.
To achieve your desired outcome, consider the following code:
This updated code employs the INSERT ... ON DUPLICATE KEY UPDATE statement in MySQL. It performs the following actions:
- Inserts new rows if the (school_art_id, baseimage_id) pair is not found.
- Updates existing rows if the (school_art_id, baseimage_id) pair already exists.
- In case of an update, only the sub_folder and layer columns are updated to the values provided in the VALUES clause.
The above is the detailed content of How to Update MySQL Data if it Exists or Insert if Not?. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn