2016-03-26 172 views
0

嗨,我有一個像下面SQL查詢範圍選擇

year | month | otherdata 
2015 1  
2015 1  
2015 3  
2015 4  
2015 4 
2016 1 
2016 2 
2016 2 

這裏我該如何選擇(2015年/ 4月至2016年/月2)
我嘗試以下查詢的所有數據表。

select * from `schedule_details` where (`year` >= 2015 and `month` >= 4) and (`year` <= 2016 and `month` >= 2) 

但它不工作。我該怎麼做呢?請幫助

+0

難道你不想在where子句的第二部分使用'month'<= 2嗎? – Lex

回答

0

感謝 問題解決了

Select * 
From schedule_details 
Where (year = 2015 And month >= 4) 
    Or (year > 2015 And year < 2016) 
    Or (year = 2016 And month <= 2) 

還有沒有其他更好的辦法?

+0

我不認爲你需要第一個「OR」條件。這一年不可能比2015年和2016年都要多。 – Lex