Cryssie
Cryssie

Reputation: 3185

PHP how to increase date incrementally in mysql

I have a bunch of tasks running on a daily basis and should be completed on the same day. So, I would like to check if these tasks finishes on the same day.

$sql = "SELECT date, MIN(date), MAX(date) FROM task_scheduler 
WHERE (date >= $date  AND date < $date_plus_1day)";

Let's say I need to check the following dates.

$min_date = '2014-12-30';
$max_date = '2015-01-03';

How can I get a daily report on the dates given in PHP like the example below:

date        MIN(date)    MAX(date)
2014-12-30  2014-12-30   2014-12-31 (didn't complete on same day)
2014-12-31  2014-12-31   2014-12-31 (completed on same day)
2015-01-01  2015-01-01   2015-01-01 (completed on same day)
2015-01-02  2015-01-02   2015-01-03 (didn't complete on same day)
2015-01-03  2015-01-03   2015-01-05 (didn't complete on same day)

Upvotes: 0

Views: 52

Answers (1)

fancyPants
fancyPants

Reputation: 51938

Use the HAVING clause. It applies after aggregate functions have been applied.

...
WHERE ...
HAVING MAX(date) = MIN(date) + INTERVAL 1 DAY;

Upvotes: 1

Related Questions