+ 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.
4 Answers
0
mysql????
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.
0
sql or mysql
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(????)