2015-02-24 99 views
0

我有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 

请有人可以帮助我这个查询?

+0

我不知道你为什么期待这个结果。您正在过滤** ** date_ini为2015或更高版本和date_end为2015或更低版本的行。换句话说,这两个字段在2015年必须有一个日期。唯一符合这些条件的行是第一个。 – Oldskool 2015-02-24 16:48:43

回答

1

如果“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') 
+0

似乎工作..但如果我想要例如2015年1月或2005年5月怎么可能?可能吗? – 2015-02-24 17:34:54

+0

是的,增加了新的查询与年份+月限制我的答案。 – Rubik 2015-02-24 17:46:15

+0

感谢似乎正在为我的例子工作。 +1为您的体验。 – 2015-02-24 19:36:46

相关问题