By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

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

P: 1
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

Expand|Select|Wrap|Line Numbers
  1. mysql_query("UPDATE table SET
  2.  
  3. COL1 = CASE WHEN COL1 = '0' then 'something' else COL1 end where ID = '$id_example',
  4. COL2 = CASE WHEN COL2 = '0' then 'something' else COL2 end where ID = '$id_example'
  5. COL3 = CASE WHEN COL3 = '0' then 'something' else COL3 end where ID = '$id_example'");
but is not working ...i don`t know why
Jun 24 '12 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,347
Please use code tags when posting code.

The correct update syntax is:
Expand|Select|Wrap|Line Numbers
  1. UPDATE table SET
  2. COL1 = CASE WHEN COL1 = '0' then 'something' else COL1 end,
  3. COL2 = CASE WHEN COL2 = '0' then 'something' else COL2 end,
  4. COL3 = CASE WHEN COL3 = '0' then 'something' else COL3 end
  5. WHERE ID = '$id_example'
Jun 24 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.