简体   繁体   中英

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?

You can use 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%'

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