MYSQL to UPDATE table if row with 2 specific columns exist or INSERT new row if it does not exist
Posted
by
user2509541
on Stack Overflow
See other posts from Stack Overflow
or by user2509541
Published on 2014-05-28T21:05:00Z
Indexed on
2014/05/28
21:26 UTC
Read the original article
Hit count: 148
I have a MYSQL table that looks as follows:
id id_jugador id_partido team1 team2
1 2 1 5 2
2 2 2 1 1
3 1 2 0 0
I need to create a query to either INSERT new rows in the table or UPDATE the table. The condition is based on id_jugador and id_partido, meaning that if I wanted to insert id_jugador = 2
and id_partido = 1
, then it should just UPDATE the existing row with the new team1 and team2 values I am sending. And dont duplicate the row.
However, if I have an entry id_jugador=2 and id_partido=3, since this combination does not exist yet, it should add the new row.
I read about the REPLACE INTO but it seems to be unable to check combination of UNIQUE KEYS.
© Stack Overflow or respective owner