5

我几乎到处找这个。我有以下 SQL 语句:

SELECT COUNT(*) FROM `job_sheet`
LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
WHERE job_sheet.completion=".ORDER_COMPLETE."
AND deliveries.ship_date>job_sheet.delivery_date

这基本上告诉我哪些已完成的工作已经过了他们的delivery_date(截止日期)。然而,真正的交货日期实际上比数据库中的提前 7 个工作日(节假日无关紧要。基本上只是跳过周末)。

我需要做的是在本节中添加 7 天:deliveries.ship_date>job_sheet.delivery_date + 7 business days

我的第一个想法是使用 DATEADD() 函数,但是我很少使用它,并且在这种情况下如何实现它非常令人困惑。

日期存储为:YYYY-MM-DD

4

3 回答 3

2

试试这个

   SELECT COUNT(*) FROM `job_sheet`
   LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
   WHERE job_sheet.completion=".ORDER_COMPLETE."
   AND  deliveries.ship_date> DATE_ADD(`job_deliveries.ship_date` , INTERVAL 7 DAY) 

ADD_DATE 的参考

于 2012-12-21T16:22:14.857 回答
1

这应该可以工作,虽然我没有测试过,因为我没有安装 mysql 实例来检查:

SELECT COUNT(*) FROM `job_sheet`
LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
WHERE job_sheet.completion=".ORDER_COMPLETE."
AND deliveries.ship_date>DATE_ADD(job_sheet.delivery_date,INTERVAL 7 DAY)
于 2012-12-21T16:25:00.390 回答
1

好吧,在我在这里的帮助下,将日期添加 7 天很容易。由于我特别需要将 7 个工作日添加到某个日期,因此我不得不在脚本的其余部分中使用一些 PHP。我使用了一个自定义 DateHelper 类来完成这项工作。

class DateHelper {

    var $holidays = array("2010-07-04", "2010-09-06", "2010-09-23", "2010-10-11", "2010-11-01", "2010-11-11",  "2010-11-25", "2010-12-25");
    const oneday = 86400; 
    const weekend = 172800; 

    function addBusinessDays($start_date, $business_days)
    {

        if (date('N', $start_date) == 6)
        { // If start date is on Saturday
            $new_start_date = $start_date + self::weekend;
        } 
        elseif (date('N', $start_date) == 7)
        { // If start date is on Sunday
            $new_start_date = $start_date + self::oneday;
        } 
        else 
        {
            $new_start_date = $start_date;
        }

        $due_date = $new_start_date + $business_days * self::oneday;
        $due_date += floor($business_days / 5) * self::weekend;

        if (($business_days % 5) + date('N', $new_start_date) >= 6)
        {
            $due_date += self::weekend; // Add 2 days to compensate for the weekend
        }

        foreach($this->holidays as $holiday)
        {
            $time_stamp = strtotime($holiday);

            // If the holiday falls between the start date and end date
            // and is on a weekday
            // Or if $new_start_date is on a holiday
            if (($start_date <= $time_stamp && $time_stamp <= $due_date && date("N", $time_stamp) < 6) || date("Y-m-d", $new_start_date) == $holiday)
            {
                $due_date += self::oneday;
                if (date('N', $due_date) >= 6)
                {
                    // If due date on Saturday or Sunday
                    $due_date += self::weekend;
                }
            }
        }

        return $due_date;
    }
}

我将这个类结合到这个:

//jobs that were shipped past their due date
public function totalShippedLate($offset = 7)
{
    $sql = mysql_query("SELECT deliveries.ship_date, job_sheet.delivery_date, deliveries.qty_shipped FROM `job_sheet` LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID WHERE job_sheet.completion=".jobInfo::ORDER_COMPLETE."") or die(mysql_error());
    $x = 0; // number of items shipped
    while($data = mysql_fetch_array($sql))
    {
        $date = new DateHelper();
        $offset_date = date("Y-m-d", $date->addBusinessDays(strtotime($data['delivery_date']), $offset));
        if($data['ship_date']>$offset_date)
        {
            $x += $data['qty_shipped']; 
        }
    }
    return $x;  
}

这将返回超过到期日发货的商品数量,并带有可自定义的偏移量

于 2012-12-21T18:11:28.497 回答