4

我的桌子上有这些字段

month
year

我想根据这个进行查询

month BETWEEN 4 AND 12 AND year = 2008

或者

month BETWEEN 1 AND 10 AND year is greater than 2008 but equal or less than 2009

我试过了,但没有给出正确的查询结果。

"WHERE month BETWEEN 4 AND 12 AND year = 2008 OR month BETWEEN 1 AND 10 AND year 2008<=2009";

注意:这里的数字是可变的,我显示了实际数字以便您轻松理解:) 有帮助吗?

year = (2008<=2009) 表示年份必须大于 2008 但等于或小于 2009

4

5 回答 5

10

试试这个:

where (year*100)+month between  200804 and  200910

这将处理所有情况..即使年份之间的差异不止一个

于 2012-09-03T08:15:45.843 回答
5
"WHERE ((month BETWEEN 4 AND 12) AND year = 2008) OR ((month BETWEEN 1 AND 10) AND (year 2008 OR 2009))";

希望它有帮助:)

于 2012-09-03T08:22:01.777 回答
2

你应该做这个,

WHERE (year = 2008 AND month BETWEEN 4 AND 12) 
       OR
      (year > ? AND year <= 2009 AND 
       month BETWEEN 1 AND 10)

你这是什么意思?greater than 2008 but equal or less than 2009. 你的意思是等于2009

于 2012-09-03T08:14:03.353 回答
1

你看过MySQL 日期函数吗?这样做就像试图用鞋钉钉子一样。

于 2012-09-03T08:13:27.653 回答
1

它应该像这样工作:

WHERE ((month BETWEEN 4 AND 12) AND year = 2008) OR ((month BETWEEN 1 AND 10) AND year = 2009)
于 2012-09-03T08:13:43.977 回答