MYSQL - How to use UPDATE with IF-ELSE condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahulephp
    New Member
    • Sep 2009
    • 59

    #1

    MYSQL - How to use UPDATE with IF-ELSE condition

    I have three columns - id, programname, status

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


    Code:
    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
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    Yes, it is possible with the following query

    [CODE=mysql]UPDATE elec_products SET
    `status` = IF(programname= 'Argos', 1, IF(programname= 'sify', 2, 3) )
    WHERE programname IS NOT NULL[/CODE]

    Comment

    Working...