简体   繁体   English

插入MySQL日期x将来的日期而不是周末

[英]Insert MySQL date x days in future and not a weekend

I have the following trigger. 我有以下触发器。 The goal is to insert a date into the alarms table that is closest to 14 days from NOW() without that date being a weekend. 目标是在警报表中插入最接近NOW()14天的日期,而该日期不是周末。 The target code begins with "INSERT INTO alarms " below. 目标代码以下面的“ INSERT INTO alarms”开头。 I'm stumped. 我很沮丧 Thanks for any help. 谢谢你的帮助。

  DELIMITER |
    CREATE TRIGGER Invoice_Funnel_AfterUpdate
        AFTER UPDATE ON INVOICE
        FOR EACH ROW BEGIN
        IF (OLD.status != NEW.status && NEW.status in ("1","5","7")
        THEN
          UPDATE customers
          SET set sales_funnel_status = '5', sales_funnel_status_date = NOW()
          WHERE customerID = OLD.customerID LIMIT 1 

INSERT INTO alarms (username, createdBy, customerID, date_stamp, alert_stamp, minutes_before, active, subject, note,timestamp,funnel,quote,chance)
              VALUES (OLD.username,OLD.username,OLD.customerID, (NOW()+ INTERVAL 14 DAY),(NOW()+ INTERVAL 14 DAY),'15','1','Post-Sales Follow-Up Alarm','Contact this customer for referrals and satisfaction survey',
              NOW(),'0','0.00',NULL)

    END;
    |
    DELIMITER ;

One sure can come up with some fancy algorithm, it's late for me, so I simply suggest: 一个人肯定可以提出一些花哨的算法,对我来说已经很晚了,所以我只建议:

NOW() + INTERVAL CASE WEEKDAY(NOW() + INTERVAL 14 DAY) WHEN 5 THEN 13 WHEN 6 THEN 12 END DAY

above is for always return friday. 以上是永远返回星期五。 Below is for always return Monday: 以下是始终返回星期一的信息:

NOW() + INTERVAL CASE WEEKDAY(NOW() + INTERVAL 14 DAY) WHEN 5 THEN 16 WHEN 6 THEN 15 END DAY

and here is always return closest: 并且总是返回最接近的:

NOW() + INTERVAL CASE WEEKDAY(NOW() + INTERVAL 14 DAY) WHEN 5 THEN 13 WHEN 6 THEN 15 END DAY

You can use WEEKDAY() or DAYOFWEEK() on now() function, which will return to you the day of the week on which event occurs. 您可以在now()函数上使用WEEKDAY()或DAYOFWEEK(),这将返回给您发生事件的星期几。

and if the day is a weekend u can add the number of needed days. 如果一天是周末,则可以添加所需的天数。

User TaTa triggered my brain cells with his answer and I came up with this as a final solution. TaTa用户用他的回答触发了我的大脑细胞,我最终提出了这个解决方案。 I am posting it as an answer to help future code warriors, but I have accepted TaTa's answer.: 我将其发布为帮助将来的代码战士的答案,但是我接受了TaTa的答案:

DELIMITER |
CREATE TRIGGER Invoice_Funnel_AfterUpdate
    AFTER UPDATE ON INVOICE
    FOR EACH ROW BEGIN
    IF (OLD.status != NEW.status && NEW.status in ("1","5","7")
    THEN
      UPDATE customers
      SET set sales_funnel_status = '5', sales_funnel_status_date = NOW()
      WHERE customerID = OLD.customerID LIMIT 1 
      INSERT INTO alarms (username, createdBy, customerID, date_stamp, alert_stamp, minutes_before, active, subject, note,timestamp,funnel,quote,chance)
      VALUES (OLD.username,OLD.username,OLD.customerID, 
      IF(dayname(date(post_date)) = 'Saturday', (NOW()+ INTERVAL 16 DAY), NOW()),
      IF(dayname(date(post_date)) = 'Sunday', (NOW()+ INTERVAL 15 DAY), NOW()),
      IF(dayname(date(alert_stamp)) = 'Saturday', (NOW()+ INTERVAL 16 DAY), NOW()),
      IF(dayname(date(alert_stamp)) = 'Sunday', (NOW()+ INTERVAL 15 DAY), NOW()),
   '15','1','Post-Sales Follow-Up Alarm','Contact this customer for referrals and satisfaction survey',
      NOW(),'0','0.00',NULL)

END;
|
DELIMITER ;

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

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