MySql - 运行触发器以在指定时间后更改列状态

时间:2021-05-27 14:36:23

I wanted to know if this is possible in Mysql and if so how.

我想知道在Mysql中是否可行,如果可行的话。

I am running an ecommerce site and when a user purchases something - on the thank you page - I want to give them a discount code with a message saying - the discount code will expire in 4 hours or 6 hours or 24 hours.

我正在运行一个电子商务网站,当用户购买东西时 - 在感谢页面上 - 我想给他们一个折扣代码,上面写着一条消息说 - 折扣代码将在4小时或6小时或24小时后到期。

Basically the logic is - once the payment is made - a unique discount code is created and inserted in this discount table as shown below

基本上逻辑是 - 一旦付款 - 创建一个唯一的折扣代码并插入此折扣表中,如下所示

discount_id | user_id | discount_code | is_active | date_created
    101     |   21    |   50$OFF      |    Y      | 9/21/2012 13:00:00 

So once the 2 hours are up - I want the trigger or event to automatically execute and change the is_active status to N

所以一旦2小时结束 - 我希望触发器或事件自动执行并将is_active状态更改为N.

discount_id | user_id | discount_code | is_active | date_created
    101     |   21    |   50$OFF      |    N      | 9/21/2012 15:00:00 

Thanks in advance

提前致谢

1 个解决方案

#1


1  

It would be better to just store an outdate timestamp of the discount like discount_until.

最好只存储折扣的过期时间戳,如discount_until。

That way you don't have to set anything.

这样你就不需要设置任何东西了。

It is the same as in the age of people. Store the birthdate in the DB, not the age, because the age can change every day.

它与人们的年龄相同。将出生日期存储在数据库中,而不是年龄,因为年龄可能每天都在变化。

#1


1  

It would be better to just store an outdate timestamp of the discount like discount_until.

最好只存储折扣的过期时间戳,如discount_until。

That way you don't have to set anything.

这样你就不需要设置任何东西了。

It is the same as in the age of people. Store the birthdate in the DB, not the age, because the age can change every day.

它与人们的年龄相同。将出生日期存储在数据库中,而不是年龄,因为年龄可能每天都在变化。