我有一个包含date_ini和date_expired的表策略,并希望根据date_ini显示策略和在2015 中过期的日期
以下是信息:
|date_ini| |date_end|
2015-01-01 2015-06-03 Here is in the period 2015
2014-02-03 2015-09-08 Here is in the period 2015
2015-06-03 2016-09-08 Here is in the period 2015
2016-01-03 2016-09-08 Here is not in the period 2015
2015-06-03 2017-09-08 Here is in the period 2015
我试过这个演示:
select * from policies where YEAR(date_ini) >= 2015 AND YEAR(date_end) <= 2015
我应该有这样的答案:
|date_ini| |date_end|
2015-01-01 2015-06-03
2014-02-03 2015-09-08
2015-06-03 2016-09-08
2015-06-03 2017-09-08
有人能帮我查询一下吗?
如果"date_ini"至少总是<=到date_end,它应该工作:
select *
from policies
where 2015 between YEAR(date_ini) AND YEAR(date_end)
您也可以将其应用于特定日期。我不是mysql专家,但如果你想限制在一个月内,这也适用:
select *
from policies
where '2015-01' BETWEEN DATE_FORMAT(date_ini, '%Y-%m')
AND DATE_FORMAT(date_end, '%Y-%m')