+ 1

I want to add a row to a database table but if a row exists with same unique key I want update that.

for example table_name=user unique_key=id insert or update firstname and lastname.

10th Jan 2017, 12:56 PM
alireza shokri
alireza shokri - avatar
4 odpowiedzi
0
mysql????
10th Jan 2017, 1:06 PM
ASNM
ASNM - avatar
0
unique_key should be NOT NULL AUTO_INCREMENT and that will handle autoupdates, you won't need to include the ID in your insert; or, you can pass an empty string and it will update for you on most engines.
10th Jan 2017, 1:07 PM
Louis Milotte
Louis Milotte - avatar
0
sql or mysql
10th Jan 2017, 1:09 PM
alireza shokri
alireza shokri - avatar
0
IF EXISTS(select ID from user where ID=1 ) THEN UPDATE user SET firstname=????,lastname=?????, WHERE ID=1; ELSE INSERT INTO user(ID, firstname,lastname) VALUES(1,?????,?????) ================== other statment ======================= INSERT INTO user(ID, firstname, lastname) VALUES(1,?,?) ON DUPLICATE KEY UPDATE firstname= VALUES(????)
10th Jan 2017, 1:16 PM
ASNM
ASNM - avatar