Updating / Enlarging Multiple MySQL Columns in a Single Query

I have this query that works ...

UPDATE `contacts` 
       SET `calls_to`=`calls_to`+1 
  WHERE `contact_no` = '0412345678';

What I also want to do is add a value to the cost field. In my opinion, a way to do this would be ...

UPDATE `contacts` 
       SET `calls_to` = `calls_to`+1, 
             `cost_to` = `cost_to`+0.25 
  WHERE `contact_no`='0412345678';

Obviously, since I am posting here, it does not work as I expected.

- UPDATE -

As requested, the table structure ..

id                  int(255) auto_increment
contact_owner  varchar(255)
contact_no       varchar(11)
contact_name   varchar(255)
calls_to            int(255)
txts_to             int(255)
time_talked_to   int(255)
cost_to            decimal(65,2)
+3
source share
4 answers

Check if the data type is for cost_toint or not. Also refresh the column if the value is not null.

UPDATE `contacts` 
       SET `calls_to` = `calls_to`+1, 
             `cost_to` = `cost_to`+0.25 
  WHERE `contact_no`='0412345678' AND
          calls_to is not null AND
          cost_to is not null;
+3
source

. cost_to? , , . ( , , 4 cost_to.)

+1

, calls_to int cost_to , . .

0

:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]

UPDATE . SET , , , . DEFAULT, . WHERE, , , , . WHERE . ORDER BY, . LIMIT , .

For syntax with multiple tables, UPDATE updates the rows in each table named table_references that satisfy the conditions. Each corresponding line is updated once, even if it meets the conditions several times. For multi-table syntax, you cannot use ORDER BY and LIMIT .

0
source

Source: https://habr.com/ru/post/1757070/


All Articles