SomeUser
SomeUser

Reputation: 2041

how to do this with pure mysql?

If mysql record with condition exists-update it,else create new record

Upvotes: 0

Views: 107

Answers (4)

Mark Byers
Mark Byers

Reputation: 839124

Use REPLACE instead of INSERT.

Upvotes: 2

BalusC
BalusC

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

mpen
mpen

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

soulmerge
soulmerge

Reputation: 75774

Are you looking for INSERT ... ON DUPLICATE KEY UPDATE?

Upvotes: 1

Related Questions