我的数据库中有下表:
CREATE TABLE `plans` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`plan` varchar(64) DEFAULT NULL,
`subscr_id` varchar(64) DEFAULT NULL,
`last_payment` timestamp NULL DEFAULT NULL,
`active` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=latin1
计划值为 1 MONTH
、3 MONTHS
和 YEAR
,分别对应 1 个月、3 个月和 12 个月。
记录示例:
id----user_id-----plan--------subcr_id-------last_payment--------active
30 | 15 | 1 MONTH | A-1 | 2017-05-19 08:22:29 |1
如果 last_payment
发生在一个多月前(如果该记录的计划是 1M
),超过 3 个月前(如果该记录的计划是 3 MONTHS)
和超过 12 个月前(如果该记录的计划是 YEAR
)。
虽然我对简单的更新语句很满意,但这个语句需要一些我不知道如何执行的 if 逻辑。你能帮帮我吗?
最佳答案
update plans set active = case
when plan = '1 MONTH' and last_payment < now() - interval 1 month then 0
when plan = '3 MONTH' and last_payment < now() - interval 3 month then 0
when plan = 'YEAR' and last_payment < now() - interval 1 year then 0
else 1
end;
关于mysql - 根据同一记录中的某个其他值更新记录值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44246306/