仅在满足条件时更新mysql表字段

时间:2014-03-25 09:17:41

标签: mysql conditional-statements

我有这个问题:

UPDATE orders SET tracking = '$trackingnumber', tracking_timestamp = NOW() 
WHERE comment like '%$ordernumber%'

如果该字段的值为0000-00-00 00:00:00,是否可以更新tracking_timestamp?

2 个答案:

答案 0 :(得分:2)

您可以使用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%'

答案 1 :(得分:0)

使用此查询

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%'