Mysql query select all dates (day & month only) within next 45 days
我需要在接下来的45天内(无论年份)选择所有日期(日和月)。 以下查询已停止工作。 就像字面上开始没有行时,肯定有数据。
有没有其他方法来执行此查询? 如果需要,很高兴在php循环中检查记录。
基本上我需要在接下来的45天内显示所有记录,即使这一年已经失效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | SELECT p.*, c.company FROM products p LEFT JOIN customers c ON c.id = p.id WHERE DATE_FORMAT(p.service_date, '%m-%d') >= DATE_FORMAT(CURDATE(), '%m-%d') AND DATE_FORMAT(p.service_date, '%m-%d') <= DATE_FORMAT((CURDATE() + INTERVAL 45 DAY), '%m-%d') AND c.email_service = 1 ORDER BY p.service_date ASC |
使用php解决了这个问题
1 2 3 4 5 6 7 8 9 10 |
并将我需要的日期直接插入查询中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | SELECT p.*, c.company FROM products p LEFT JOIN customers c ON c.id = p.id WHERE DATE_FORMAT(p.service_date, '%m-%d') IN ( $DateQuery ) AND c.email_service = 1 ORDER BY p.service_date ASC |
解决此问题的最佳方法是使用
1 2 3 4 5 6 | CASE WHEN DAYOFYEAR('2018-12-31')-DAYOFYEAR(NOW()) < 45 THEN DAYOFYEAR(p.service_date) BETWEEN DAYOFYEAR(NOW()) AND DAYOFYEAR('2018-12-31') OR DAYOFYEAR(p.service_date) BETWEEN 1 AND DAYOFYEAR(NOW() + INTERVAL 45 DAY) ELSE DAYOFYEAR(p.service_date) BETWEEN DAYOFYEAR(NOW()) AND DAYOFYEAR(NOW() + INTERVAL 45 DAY) END |
您的完整查询应如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | SELECT p.*, c.company FROM products p LEFT JOIN customers c ON c.id = p.id WHERE (CASE WHEN DAYOFYEAR('2018-12-31')-DAYOFYEAR(NOW()) < 45 THEN DAYOFYEAR(p.service_date) BETWEEN DAYOFYEAR(NOW()) AND DAYOFYEAR('2018-12-31') OR DAYOFYEAR(p.service_date) BETWEEN 1 AND DAYOFYEAR(NOW() + INTERVAL 45 DAY) ELSE DAYOFYEAR(p.service_date) BETWEEN DAYOFYEAR(NOW()) AND DAYOFYEAR(NOW() + INTERVAL 45 DAY) END) AND c.email_service = 1 ORDER BY p.service_date ASC |
如果您担心闰年,可以将
我不知道为什么你的原始查询在已经存储在MySQL Date类型的列(
您只需要在日期中添加/减去
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | SELECT p.*, c.company FROM products p LEFT JOIN customers c ON c.id = p.id WHERE p.service_date >= CURDATE() AND p.service_date <= (CURDATE() + INTERVAL 45 DAY) AND c.email_service = 1 ORDER BY p.service_date ASC |