If mysql record with condition exists-update it,else create new record
+5
A:
Is this what you're looking for?
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Mark
2009-11-29 20:00:21
That is ok but will only do what the OP wanted if there is a unique index on those columns; it can't be used for arbitrary conditions.
MarkR
2009-11-29 21:52:09
+2
A:
You can use the REPLACE INTO
for that with the syntax of INSERT INTO
. This way MySQL will invoke an UPDATE
whenever there's a fictive constraint violation.
Be aware that this construct is MySQL-specific, so your query ain't going to work whenever you switch from DB.
BalusC
2009-11-29 20:02:52