Reputation: 613
I have a table that has three fields
field_one field_two field_three
I'd like to do an insert/update, but instead of checking if one of the key fields already exists, I need to check if (field_one,field_two) combination is already in the database, if so, then update instead of inserting.
Upvotes: 3
Views: 1231
Reputation: 28834
Sound to me like you can use REPLACE INTO
or ON DUPLICATE KEY UPDATE
as long as there is a unique constraint on the two fields.
MySql doesn't support the MERGE statement, so need either a unique constraint or some external code.
Upvotes: 0
Reputation: 92752
Create unique index your_index_name on yourtable (field_one,field_two)
(see docs) and use INSERT...ON DUPLICATE KEY UPDATE.
MySQL will do the rest automagically.
Upvotes: 5
Reputation: 5443
Multiple ways to do this. Easiest is probably something like this:
Upvotes: 0