SBSTP
SBSTP

Reputation: 3639

MYSQL if/else clause

I need to do a such thing in SQL but I don't know how...

IF *row exists* THEN  
  UPDATE ...
ELSE
 CREATE ...

I can't figure how to do that..

Upvotes: 0

Views: 560

Answers (2)

John Boker
John Boker

Reputation: 83699

You can use a normal insert statement with a ON DUPLICATE KEY UPDATE described in the docs :

here http://dev.mysql.com/doc/refman/5.0/en/insert.html

Upvotes: 0

Joshua Martell
Joshua Martell

Reputation: 7212

INSERT ... ON DUPLICATE KEY UPDATE.... or REPLACE should do the trick:

ON DUPLICATE KEY UPDATE http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

REPLACE http://dev.mysql.com/doc/refman/5.0/en/replace.html

Upvotes: 4

Related Questions