MySQL update column which is a value in another column

Pankaj Singhal

This is my previous question related to the my query. MySQL select column which is a value in another column

The problem is that want to do operations on the values extracted and store it back into the original db. I've tried using a update & case but am not able to achieve it.

update msisdn_table
   CASE reason
     WHEN 'NoAnswer' THEN (case when (NoAnswer>0) then update msisdn_table set NoAnswer = NoAnswer-1 end)
     WHEN 'NetworkBusy' THEN (case when NetworkBusy>0 then update msisdn_table set NetworkBusy = NetworkBusy-1 end)
     WHEN 'CallRejection' THEN (case when CallRejection>0 then update msisdn_table set CallRejection = CallRejection-1 end)
     WHEN 'Unavailable' THEN (case when Unavailable>0 then update msisdn_table set Unavailable = Unavailable-1 end)
   END 

Any help?

peterm

Try it this way if you want to do it one statement

UPDATE msisdn_table
   SET NoAnswer      = IFNULL(IF(reason = 'NoAnswer',      
                                 NULLIF(NoAnswer,      0) - 1, NoAnswer), 0), 
       NetworkBusy   = IFNULL(IF(reason = 'NetworkBusy',   
                                 NULLIF(NetworkBusy,   0) - 1, NetworkBusy), 0), 
       CallRejection = IFNULL(IF(reason = 'CallRejection', 
                                 NULLIF(CallRejection, 0) - 1, CallRejection), 0), 
       Unavailable   = IFNULL(IF(reason = 'Unavailable',   
                                 NULLIF(Unavailable,   0) - 1, Unavailable), 0)
  WHERE reason IN('NoAnswer', 'NetworkBusy', 'CallRejection', 'Unavailable');

Note:

  1. I changed CASE with less verbose IF(), although if you like it better you can use it the same way.
  2. This approach has one possible side effect as it always updates the column(s) either with a new or with old value. It may matter if for example you have a trigger defined on the table.
  3. You want to apply a WHERE clause to make sure that rows with other reason codes are not affected

Here is SQLFiddle demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL update column which is a value in another column

From Dev

mysql update column value from another table

From Dev

MySQL update column value with max value from another column

From Dev

update a column with derived value from another column in mysql

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

update a column with derived value from another column in mysql

From Dev

Update value in column based on column count in another table with mysql

From Dev

How to update mysql column with another value from another table?

From Dev

How to update mysql column with another value from another table?

From Dev

Update MySQL table taking the value of a column, adding it to another column, the changing the value of the first column

From Dev

MySQL update column value where else update another column value (single query)

From Dev

MySQL - if value in column then insert value in another column

From Dev

Update column from another table column value

From Dev

How to update a column using another column value?

From Java

MySQL: update column with data from another column

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update column in one table based on value in another table in mysql

From Dev

update value in column and have a trigger update another column depending on the value

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

sql get column value which all values in another column no value

From Dev

Column loop and update another column with COUNT() value from another table

From Dev

Update column value by querying concatenated column values with another table value

From Dev

Update column value based on PHP variable which is equal to a column name

From Dev

How to create a column which has the "+" or "-" depending on the value in another column?

From Dev

How to update a column with another tables columns value?

From Dev

Update newly added column with value on another table

From Dev

MySQL update md5 of another column

From Dev

mySQL update column with concat of another table

From Dev

mysql update based on time stamp in another column

Related Related

  1. 1

    MySQL update column which is a value in another column

  2. 2

    mysql update column value from another table

  3. 3

    MySQL update column value with max value from another column

  4. 4

    update a column with derived value from another column in mysql

  5. 5

    Update value in a column based on another column in the same table in MYSQL

  6. 6

    update a column with derived value from another column in mysql

  7. 7

    Update value in column based on column count in another table with mysql

  8. 8

    How to update mysql column with another value from another table?

  9. 9

    How to update mysql column with another value from another table?

  10. 10

    Update MySQL table taking the value of a column, adding it to another column, the changing the value of the first column

  11. 11

    MySQL update column value where else update another column value (single query)

  12. 12

    MySQL - if value in column then insert value in another column

  13. 13

    Update column from another table column value

  14. 14

    How to update a column using another column value?

  15. 15

    MySQL: update column with data from another column

  16. 16

    Update column in one table based on value in another table in mysql

  17. 17

    Update column in one table based on value in another table in mysql

  18. 18

    update value in column and have a trigger update another column depending on the value

  19. 19

    UPDATE sql column with value from another column based on a date column

  20. 20

    sql get column value which all values in another column no value

  21. 21

    Column loop and update another column with COUNT() value from another table

  22. 22

    Update column value by querying concatenated column values with another table value

  23. 23

    Update column value based on PHP variable which is equal to a column name

  24. 24

    How to create a column which has the "+" or "-" depending on the value in another column?

  25. 25

    How to update a column with another tables columns value?

  26. 26

    Update newly added column with value on another table

  27. 27

    MySQL update md5 of another column

  28. 28

    mySQL update column with concat of another table

  29. 29

    mysql update based on time stamp in another column

HotTag

Archive