i want to insert rows IF a row containing the specific values exists, and if not update it.
Concretely:
A column user_id=5, user_zip=12345, distance=600 exists on the database.
If i try to insert user_id=5, user_zip=12345, distance=700 it should just update the distance
but i try to insert user_id=5, user_zip=67890, distance=800 it should insert a new row.
I can’t define the columns user_zip and distance unique, so that i can use the on duplicate key update.
I think you are misunderstanding how
ON DUPLICATE KEY UPDATEworks. With a unique constraint on(user_id, user_zip), this should work:You don’t have to define user_zip unique (that’s how I understand your question), just the combination of
user_idanduser_zip. So you still can have 2 or more rows with the sameuser_id, just theuser_zipcan’t match on those rows.