2015-02-24 125 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

相關問題