MySQL在2天之前得到没有计算周末

时间:2018-01-29 13:32:08

标签: php mysql

有没有办法在2天前在MySQL计算而不计算周六和周日?我的数据样本:

<script type="text/javascript">
  (function($) {
    $(document).ready(function() {

      $("#accordion li.active").addClass("open").children("ul").show();
      $("#accordion li.has-sub>a").on("click", function() {
        $(this).removeAttr("href");
        var element = $(this).parent("li");
        if (element.hasClass("open")) {
          element.removeClass("open");
          element.find("li").removeClass("open");
          element.find("ul").slideUp(200);
        } else {
          element.addClass("open");
          element.children("ul").slideDown(200);
          element.siblings("li").children("ul").slideUp(200);
          element.siblings("li").removeClass("open");
          element.siblings("li").find("li").removeClass("open");
          element.siblings("li").find("ul").slideUp(200);
        }
      });

    });
  })(jQuery);
</script>

<div id="accordion">
  <ul>
    <li class="has-sub"><a href="#">15:00</a>
      <ul>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      </ul>
    </li>
    <li class="has-sub"><a href="#">15:00</a>
      <ul>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      </ul>
    </li>
    <li class="has-sub"><a href="#">17:00</a>
      <ul>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      </ul>
    </li>
    <li class="has-sub"><a href="#">19:00</a>
      <ul>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      </ul>
    </li>
  </ul>
</div>

当我在Wednesday, 20 Sept 2017 Thursday, 21 Sept 2017 Friday, 22 Sept 2017 Saturday, 23 Sept 2017 Sunday, 24 Sept 2017 Monday, 25 Sept 2017 Tuesday, 26 Sept 2017 Wednesday, 27 Sept 2017 上运行查询时,我会获得第一个数据(Friday, 22 Sept 2017

当我在Wednesday, 20 Sept 2017上运行查询时,我会获得第二个数据(Saturday, 23 Sept 2017

当我在2017年9月24日星期日运行查询时,我将获得第三个数据(Thursday, 21 Sept 2017

但是当我在星期一运行查询时,我没有获得任何数据

当我在星期二运行查询时,我将获得第四个数据(Friday, 22 Sept 2017

当我在星期三运行查询时,我将获得第五个数据和第六个数据(Saturday, 23 Sept 2017

到目前为止我的查询:

Sunday, 24 Sept 2017 && Monday, 25 Sept 2017

它可以在2天之前计算,但它仍然计算在周六和周日。基本上,我创建一个每天运行的查询,以自动更改状态为2天的数据。例如,在星期三,我将更改星期一有SELECT * FROM oc_order WHERE (status_id = '2' AND date_added LIKE CONCAT(DATE_ADD(DATE(NOW()), INTERVAL -2 DAY),'%')) OR (status_id = '21' AND date_modified LIKE CONCAT(DATE_ADD(DATE(NOW()), INTERVAL -2 DAY),'%')) 的任何数据(例如)。换句话说,星期六的数据将在星期二处理。周日数据将在周三处理,包括周一数据,也将在周三处理

2 个答案:

答案 0 :(得分:0)

SELECT * FROM   (SELECT status_id,
    date_added,
    date_modified,
    CASE
      WHEN DAYOFWEEK(now()) = 2
      THEN CONCAT(DATE_ADD(DATE(NOW()), INTERVAL -3 DAY),'%')
      WHEN DAYOFWEEK(now()) = 3
      THEN CONCAT(DATE_ADD(DATE(NOW()), INTERVAL -4 DAY),'%')
      ELSE CONCAT(DATE_ADD(DATE(NOW()), INTERVAL -2 DAY),'%')
    END DAY_OF_WEEK   FROM oc_order   ) WHERE (status_id = '2' AND date_added LIKE DAY_OF_WEEK) OR (status_id = '21' AND date_modified LIKE DAY_OF_WEEK)

答案 1 :(得分:0)

最后,我所做的是在星期一到星期五之间设置cron工作,所以星期六和星期日不计算在内。此外,这些工作在PHP中更好,而不是在SQL中。感谢您的任何评论和回答