MYSQL - How to use UPDATE with IF-ELSE condition

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
rahulephp
Forum Commoner
Posts: 28
Joined: Mon Oct 05, 2009 11:05 am

MYSQL - How to use UPDATE with IF-ELSE condition

Post by rahulephp »

I have three columns - id, programname, status

I wanted to use UPDATE with IF condition something like this:

Code: Select all

UPDATE elec_products
	IF(programname ='Argos')	
	(
	    SET status = 1   
	)
	ELSEIF(programname ='sify')
	(
	    SET status = 2  
	)
	ELSE
	(
	    SET status = 3  
	)
	ENDIF
WHERE programname IS NOT NULL

I am not sure if it'll be possible. Please suggest the best possible ways to do this.
Thank you
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: MYSQL - How to use UPDATE with IF-ELSE condition

Post by Benjamin »

You have to nest them. You'll probably want to use CASE instead. Although it's very rare that one would need to write such a query.

See: http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: MYSQL - How to use UPDATE with IF-ELSE condition

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Post Reply