[英]Updating mysql table field only when condition is met
I have this query: 我有这个查询:
UPDATE orders SET tracking = '$trackingnumber', tracking_timestamp = NOW()
WHERE comment like '%$ordernumber%'
Is it possible to update tracking_timestamp ONLY if the value of the field is 0000-00-00 00:00:00? 如果该字段的值是0000-00-00 00:00:00,是否可以仅更新tracking_timestamp?
You can use CASE
您可以使用
CASE
UPDATE orders
SET tracking = '$trackingnumber',
tracking_timestamp = CASE
WHEN tracking_timestamp='0000-00-00 00:00:00'
THEN NOW()
ELSE tracking_timestamp
END
WHERE comment like '%$ordernumber%'
use this query 使用此查询
UPDATE orders
SET tracking = '$trackingnumber',
tracking_timestamp = IF (tracking_timestamp='0000-00-00 00:00:00',NOW(),'0000-00-00 00:00:00')
WHERE COMMENT LIKE '%$ordernumber%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.