Home > database >  Mysql in the use of the update and case modification calculation result is different
Mysql in the use of the update and case modification calculation result is different

Time:10-10

I recently met a mysql problem!
Columns such as: the update goods set rate=(case id when 1 then the when 0.9 2 then the when 3 then 0.5 0.8 else rate), money=price * rate where id in (1, 2, 3); Such a SQL statement, rate before the execution is 1, the results of calculation are correct before, recently don't know what's wrong, the results of calculation are not correct, what values are no values of execution to calculate, this led to the calculation result is 1 * price of money, the amount of discounts and corresponding. Be badly in need of solving this is why,

CodePudding user response:

You select out
Select
(case id when 1 then the when 0.9 2 then the when 3 then 0.5 0.8 else rate), money=price * rate where id in (1, 2, 3);

CodePudding user response:

Performed separately, so that efficiency can improve a bit,
The UPDATE goods SET rate=0.9, money=price * rate WHERE id=1;
The UPDATE goods SET rate=0.8, money=price * rate WHERE id=2;
The UPDATE goods SET rate=0.5, money=price * rate WHERE id=3;

CodePudding user response:

There is nothing wrong with the SQL statement
You see you what type of, can't save the decimal?

CodePudding user response:

Script found in the following two mistakes:
1. Used as A place, A letter is A total solution enter
2. AS B, in parentheses in front of the AS B, already has a AS W
Statements nested a bit more, it is recommended to use the following format:
Select A. *
The FROM score AS A
LEFT the JOIN course AS AC ON A.c ourse_id=AC. Cid
LEFT the JOIN score AS B ON A.s tudent_id=B.s tudent_id
LEFT the JOIN course AS BC ON biggest ourse_id=BC. Cid
WHERE AC. Cname=N 'physical' AND BC. Cname=N 'biological'

CodePudding user response:

Get out? What is the specific situation?
  • Related