我有一张表 placed_order
,其中包含 order_time
(时间戳)、estimated_delivery_time
(时间戳)等列。在第二个表 addresses
中有列 zone
(除了其他)。
zone
列是一个 ENUM 字段,包含值“Zone 1”、“Zone 2”、“Zone 3”。
现在我需要计算estimated_delivery_time
。根据区域的不同,它是 order_time
加上 10 分钟(区域 1)、15 分钟(区域 2)或 20 分钟(区域 3)。
我已经找到了如何通过手动添加分钟来计算 estimated_delivery_time
:
UPDATE `placed_order`
SET `estimated_delivery_time` = DATE_ADD(`order_time`, INTERVAL 10 MINUTE)
WHERE placed_order_id = 9;
但我一直在努力寻找如何根据区域计算 estimated_delivery_time
的解决方案。我已经尝试过使用 SELECT IF 和 CASE 子句但找不到方法。有什么建议吗?
最佳答案
您可以使用 Case .. When
更新 estimated_delivery_time
。尝试以下查询:
UPDATE `placed_order` AS po
JOIN `addresses` AS addr ON addr.customer_id = po.customer_id
SET po.estimated_delivery_time = CASE addr.zone
WHEN 'Zone 1'
THEN DATE_ADD(po.order_time, INTERVAL 10 MINUTE)
WHEN 'Zone 2'
THEN DATE_ADD(po.order_time, INTERVAL 15 MINUTE)
WHEN 'Zone 3'
THEN DATE_ADD(po.order_time, INTERVAL 20 MINUTE)
END
关于mysql - 从依赖于另一个表中 ENUM 列的值的表中更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52920638/