使用php / SQL查询数据库中的信息

database columns

I want to select data from a table using those two columns. I'm i trying to do is select the table where month is between February and August and year between 2003 and 2005 including the month of january for 2004 and 2005. I have tried this: `

$number = range(3, 8);  

foreach($number as $key=> $value):
                    $where .= " Month ='$value' AND Year BETWEEN '2003' AND '2005' or " ;                   
        endforeach;
if(strlen(trim($where)) > 0)

    $where = substr(trim($where), 0, -2);`
$query = "SELECT * FROM myTable $where";

which return the following query

SELECT * FROM myTable WHERE Month ='2' AND Year BETWEEN '2003' AND '2005' or Month ='3' AND Year BETWEEN '2003' AND '2005' or Month ='4' AND Year BETWEEN '2003' AND '2005' or Month ='5' AND Year BETWEEN '2003' AND '2005' or Month ='6' AND Year BETWEEN '2003' AND '2005' or Month ='7' AND Year BETWEEN '2003' AND '2005' or Month ='8' AND Year BETWEEN '2003' AND '2005'

But the problem with my query is it is not selecting january of 2004 and 2005. Any one with an idea of how to do this?

SELECT * FROM myTable 
WHERE ( Month >=2 AND Month <=8 AND year BETWEEN 2003 AND 2005) OR (Month=1 AND (year=2004 OR year=2005))

Change your query like below..

SELECT * FROM myTable 
WHERE 
(Month ='2' AND Year BETWEEN '2003' AND '2005') 
or
( Month ='3' AND Year BETWEEN '2003' AND '2005')....

The precedence of operators determines the order of evaluation of terms in an expression. To override this order and group terms explicitly, use parentheses