简体   繁体   English

如何每 20 和 30 分钟更新一次值?

[英]How can I update value every 20 and 30 minutes?

I have a table 'comenzi', each order has a value for in progress, and another for ready and data.我有一个表“comenzi”,每个订单都有一个进行中的值,另一个用于准备和数据。 Each order when it is created has the values for in progress and ready equal to 0. I want to update the values for in progress and ready with 1 min ,after 20 min, then 30 min after the order has been done, the value from data that has the form 2020-05-25 09:41:00.每个订单在创建时的 in progress 和 ready 值都等于 0。我想更新 in progress 和 ready 的值,时间为 1 分钟,20 分钟后,然后是订单完成后 30 分钟,值来自具有 2020-05-25 09:41:00 形式的数据。

I need a way to update these values, a function or an event in database.我需要一种方法来更新这些值、数据库中的函数或事件。

I was dealing with something using events just yesterday on a pet project.昨天我在一个宠物项目中使用事件处理一些事情。 You could use a similar approach.您可以使用类似的方法。

This code uses a creation column, which is just a TIMESTAMP column with a default value of CURRENT_TIMESTAMP (because I don't have your data) and a value column (default value 0), which goes from 1 - 2 (you can modify this code to suit your needs).这段代码使用了一个creation列,它只是一个TIMESTAMP列,默认值为CURRENT_TIMESTAMP (因为我没有你的数据)和一个value列(默认值 0),它从 1 - 2(你可以修改这个代码以满足您的需求)。

The SET value uses CASE s to check how long ago the order was created. SET值使用CASE来检查订单创建的时间。 Then it updates the value accordingly.然后它相应地更新值。 You can change up the event to update whichever columns / time cases you'll need.您可以更改事件以更新您需要的任何列/时间情况。

CREATE DEFINER =`root`@`localhost` EVENT `progressEvent`
  ON SCHEDULE EVERY 1 MINUTE STARTS '2020-09-01 00:00:00'
  ON COMPLETION PRESERVE ENABLE DO UPDATE comenzi
SET `value` = (
  CASE WHEN (creation < (CURRENT_TIMESTAMP - INTERVAL 30 MINUTE))
  THEN 2
  CASE WHEN (creation < (CURRENT_TIMESTAMP - INTERVAL 20 MINUTE))
  THEN 1
  END)
WHERE value < 2

Edit: Changed it up so that it checks every minute and only 20/ 30 minutes.编辑:将其更改为每分钟检查一次,并且仅检查 20/30 分钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM