Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Different UPDATE else INSERT statement

Tags:

php

mysql

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?

like image 674
Anriëtte Myburgh Avatar asked Apr 08 '26 10:04

Anriëtte Myburgh


1 Answers

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.

like image 181
Tyler Carter Avatar answered Apr 10 '26 00:04

Tyler Carter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!