简体   繁体   中英

mysql update column based on another updated column

Note, this question is not same to the MySQL update column based on another column data manipulation

table order

| order_number | payment_amount | refund_amount | refund_status |
| 12           |  100           | 50            | 1             |

refund_status:

0: none refunded;
1: part refunded;
2: all refunded;
update order set refund_amount = refund_amount + 50, refund_status = CASE WHEN payment_amount = refund_amount + 50 THEN 2 ELSE 1 END where order_number = 12;
// refund_amount -> 100, refund_status ->1

Cannot get the right result (refund_amount -> 100, refund_status ->2).

But

update order set refund_amount = refund_amount + 50, refund_status = CASE WHEN payment_amount = refund_amount THEN 2 ELSE 1 END where order_number = 12;
// refund_amount -> 100, refund_status ->2

Can get the right result.

It seems update refund_status besed on the updated refund_amount , is it reliable?

use bracket every you use function case for system readable on your logic code.

update order
set refund_amount = (refund_amount + 50),
refund_status = (CASE WHEN payment_amount = refund_amount + 50 THEN 2 ELSE 1 END)
where order_number = 12;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM