Reputation: 2041
If mysql record with condition exists-update it,else create new record
Upvotes: 0
Views: 107
Reputation: 1109665
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.
Upvotes: 2
Reputation: 283313
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
Upvotes: 5