INSERT if UPDATE's WHERE clause returns 0

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

INSERT if UPDATE's WHERE clause returns 0

Post by anjanesh »

I have

Code: Select all

UPDATE `table1` SET `field1` = 'a' WHERE `ID` = '123'
If 123 doesnt exist then create (INSERT) it.
Is there a way to do this in one SQL statement in MySQL ?

Thanks
Last edited by anjanesh on Tue Aug 02, 2005 10:36 am, edited 1 time in total.
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

It is possible ...........but you need to use stored procedures for that ........ you might want to look at stored procedures for mysql
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Although it seems the other way round, it does what you want ;)

http://dev.mysql.com/doc/mysql/en/replace.html
Post Reply