Anriëtte Myburgh
Anriëtte Myburgh

Reputation: 13517

MySQL - Different UPDATE else INSERT statement

I am busy doing an UPDATE/INSERT request, but here is the crux:

table PLAYERS {
    player_id
    game_id
    team_id
    position
    number
}

What is supposed to happen is the following:

I test if there is an entry where player_id = '$player_id' AND game_id = '$game_id' AND team_id = '$team_id'.

If there is, then the following happens:

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

Is there any way I can do this using just MySQL query language, without the need for PHP validation between queries?

Upvotes: 3

Views: 953

Answers (3)

HaveAGuess
HaveAGuess

Reputation: 1241

That will not work unless its in a transaction, or you can guarantee that simultaneous requests against the DB will not be run

Upvotes: 0

Anriëtte Myburgh
Anriëtte Myburgh

Reputation: 13517

Well, I fixed this with multiple IF-statements to determine whether to insert or to update.

Upvotes: 0

Tyler Carter
Tyler Carter

Reputation: 61567

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

1) Tries to Insert
2) If there is a record with a unique field (Primary Key, Unique Index, etc) update that field instead.

Upvotes: 6

Related Questions