如何根据一年的时间显示日期?

时间:2015-02-24 16:36:45

标签: mysql

我有一个包含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 

我试过这个demo

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

请有人帮我解决这个疑问吗?

1 个答案:

答案 0 :(得分:1)

如果“date_ini”至少总是&lt; = to 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')