Update MySql Column (if field is not empty, go to next one)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jhon
    New Member
    • Jun 2012
    • 1

    #1

    Update MySql Column (if field is not empty, go to next one)

    Hello I have a table with 4 columns (ID, COL1, COL2, COL3) where (COL1 COL2 COL3) is default "0" , i want to make an update with php in the following way.

    If COL1=0 make the updade in COL1
    If COL1 is not=0 then go to COL2 and chek if it 0, and then update it.

    So if the first column has anything else then "0" update the nex column.

    Until ow i have something like this

    Code:
    mysql_query("UPDATE table SET
    
    COL1 = CASE WHEN COL1 = '0' then 'something' else COL1 end where ID = '$id_example',
    COL2 = CASE WHEN COL2 = '0' then 'something' else COL2 end where ID = '$id_example'
    COL3 = CASE WHEN COL3 = '0' then 'something' else COL3 end where ID = '$id_example'");
    but is not working ...i don`t know why
    Last edited by Rabbit; Jun 24 '12, 04:51 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    The correct update syntax is:
    Code:
    UPDATE table SET
    COL1 = CASE WHEN COL1 = '0' then 'something' else COL1 end,
    COL2 = CASE WHEN COL2 = '0' then 'something' else COL2 end,
    COL3 = CASE WHEN COL3 = '0' then 'something' else COL3 end
    WHERE ID = '$id_example'

    Comment

    Working...