Page 1 of 1

MYSQL - How to use UPDATE with IF-ELSE condition

Posted: Fri Oct 15, 2010 4:54 am
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

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

Posted: Fri Oct 15, 2010 5:13 am
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

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

Posted: Fri Oct 15, 2010 7:19 am
by VladSun