INSERT ... ON DUPLICATE KEY UPDATE ..... with two key fields

I have a table with three fields

field_one field_two field_three

I would like to do an insert / update, but instead of checking if one of the key fields exists, I need to check if the (field_one, field_two) combination is already in the database, if so, the insert.

+3
source share
3 answers

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 automatically.

+5
source

Several ways to do this. The easiest way is probably something like this:

  • Get existing fields
  • ,
0

Sound for me, as you can use REPLACE INTOor ON DUPLICATE KEY UPDATE, as long as there is a unique restriction for two fields.

MySql does not support the MERGE statement, so either a unique constraint or some kind of external code is required.

0
source

Source: https://habr.com/ru/post/1776792/


All Articles