尝试编写一个查询,该查询将返回任务的开始日期和结束日期介于发布日期之间的所有数据,如果设置了开始日期但结束日期为0000-00-00
,那么数据将继续出现在结果中,因为它没有结束日期。此外,结果应该显示我们当周所在的当周,即NOW()
。因此,如果我们比较发布日期和当前周的周数和发布日期周数较高,则结果应该会出现。
我的数据库结构如下所示:
id cons_id day_name start_date end_date status_code
___________________________________________________________________________
1 105 Wednesday 2017-05-17 0000-00-00 0
2 106 Wednesday 2017-05-24 2018-06-20 0
3 107 Thursday 2017-05-25 2030-02-28 0
4 108 Friday 2017-05-19 2019-01-25 0
5 109 Wednesday 2018-01-03 2018-02-14 0
我的查询如下:
SELECT i.cons_id AS 'cons_id',
i.type AS 'type',
i.day_name AS 'day_name',
i.day_num AS 'day_num',
i.start_date AS 'start_date',
i.end_date AS 'end_date',
i.created_at AS 'created_at',
i.updated_at AS 'updated_at'
FROM recurent_cons AS i
WHERE i.status_code = '0'
AND ('2017-05-26' BETWEEN i.start_date AND i.end_date)
AND i.end_date >= '2017-05-26'
OR i.end_date = '0000-00-00'
上面的查询显示id
1的记录即使在当前一周也不对,当我将日期更改为年2018
时,它也不会显示今年的记录{{ 1}}记录的结束日期是2017
所以结果将是这样的:
如果我选择2017-05-24,我应该看到:
2018
id cons_id day_name start_date end_date status_code
排除当前日期并显示未来日期加上无穷大日期(1 105 Wednesday 2017-05-17 0000-00-00 0
3 107 Thursday 2017-05-25 2030-02-28 0
5 109 Wednesday 2018-01-03 2018-02-14 0
)
答案 0 :(得分:0)
对于OR i.end_date = '0000-00-00'
条目的最后id=1
为真,这就是为什么它出现在您的查询结果中。
您可能想要这样做:
SELECT i.cons_id AS 'cons_id',
i.type AS 'type',
i.day_name AS 'day_name',
i.day_num AS 'day_num',
i.start_date AS 'start_date',
i.end_date AS 'end_date',
i.created_at AS 'created_at',
i.updated_at AS 'updated_at'
FROM recurent_cons AS i
WHERE i.status_code = '0'
AND ('2017-05-26' BETWEEN i.start_date AND i.end_date)
AND (i.end_date >= '2017-05-26' OR i.end_date = '0000-00-00')
如果您想要所有当前日期,未来或尚未结束的条目:
SELECT i.cons_id AS 'cons_id',
i.type AS 'type',
i.day_name AS 'day_name',
i.day_num AS 'day_num',
i.start_date AS 'start_date',
i.end_date AS 'end_date',
i.created_at AS 'created_at',
i.updated_at AS 'updated_at'
FROM recurent_cons AS i
WHERE i.status_code = '0'
AND (i.start_date >='2017-05-26' # Future and current
OR i.end_date = '0000-00-00') # Didn't finish yet