MySQL is another UPDATE else INSERT statement

I'm busy doing an UPDATE / INSERT query, but here is the gist:

table PLAYERS {
    player_id
    game_id
    team_id
    position
    number
}

This is supposed to happen:

I check if there is a record where player_id = '$player_id' AND game_id = '$game_id' AND team_id = '$team_id'.

If so, the following will happen:

position = '$position' AND number = '$number'

Is there a way to do this using only the MySQL query language, without having to check PHP between requests?

+3
source share
3 answers

Well, I fixed it with a few IF statements to determine whether to insert or update.

0
source
INSERT INTO TABLE (COLUMNS) VALUES(FIELDS) UPDATE ON DUPLICATE KEY position = somevalue, number=number

1) tries to insert text 2) If there is a record with a unique field (Primary key, Unique index, etc.), Update this field.

+6
source

, , ,

0

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


All Articles