php mysql查询语法

时间:2012-09-03 08:11:28

标签: php mysql sql

我的桌子上有这些字段

month
year

我想根据此

进行查询
month BETWEEN 4 AND 12 AND year = 2008

OR

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&lt; = 2009)意味着年份必须大于2008但等于或小于2009

5 个答案:

答案 0 :(得分:10)

试试这个:

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

这将照顾所有场景..即使这些年份之间的差异不止一个

答案 1 :(得分:5)

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

希望它有所帮助:)

答案 2 :(得分: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吗?

答案 3 :(得分:1)

你看过MySQL Date Functions了吗?这样做就像试着用鞋钉在钉子上。

答案 4 :(得分:1)

它应该像这样工作:

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