Mysql列表日期范围之间的日期,但仅限于一周中的特定日期
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql列表日期范围之间的日期,但仅限于一周中的特定日期相关的知识,希望对你有一定的参考价值。
我使用下面的代码获取日期范围之间的日期列表。
SELECT ADDDATE('2012-02-10', INTERVAL @i:=@i+1 DAY) AS DAY
FROM (
SELECT a.a
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
JOIN (SELECT @i := -1) r1
WHERE
@i < DATEDIFF('2012-02-15', '2012-02-10')
现在我只想列出与一周中特定日期相匹配的日期(.i.e。只有在日期是星期一或星期四时它才会返回)。
有任何想法吗?
答案
使用星期一开始的函数WEEKDAY()来计算它。
e.g:
where WEEKDAY(DAY) = 'Monday';
超过一天使用in关键字:
where WEEKDAY(DAY) in ('Monday', 'Tuesday','Friday');
当你使用别名时,你必须使用而不是在哪里:
having WEEKDAY(DAY) in ('Monday', 'Wednesday');
最终查询:
SELECT ADDDATE('2018-03-01', INTERVAL @i:=@i+1 DAY) AS DAY
FROM (
SELECT a.a
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c
) a
JOIN (SELECT @i := -1) r1
WHERE
@i < DATEDIFF('2018-03-20', '2018-03-01') having WEEKDAY(DAY) in
('Monday', 'Wednesday');
以上是关于Mysql列表日期范围之间的日期,但仅限于一周中的特定日期的主要内容,如果未能解决你的问题,请参考以下文章
如何在 oracle 中计算工作日,不包括一周中的任何特定日期?
如何根据 PHP 数据库中的结果在 jQuery 日期选择器上禁用一周中的某些天
Dijit DateTextBox 上的限制 [一周中的有效日期]