I have a SQL Database. In here is a table called Reports
and it looks like this
ID Date Year Title Links
1 2010-05-03 2010 Report 1 link1.php
2 2010-09-03 2010 Report 2 link2.php
3 2011-01-05 2011 Report 3 link3.php
What I'm trying to achieve it the this. I want to select all the reports of 2010-2011 but the ones from 2010 may only be those dating from september.
Right now, I've got a SQL statement selecting all the reports of the two years
$sql = "SELECT * FROM Reports WHERE Year = ".$db->escape($jaar1)." OR jaar = ".$db->开发者_开发技巧;escape($jaar2);
How can I select Report 2
(cause it dates from september 2010) and Report 3
(cause it dates from 2011)?
The easiest way as I see it, is to use the BETWEEN...AND
operator.
SELECT *
FROM `Reports`
WHERE `Date` BETWEEN '2010-09-01' AND NOW()
Note the backticks. Especially important around Date
, because it is a reserved word.
Assuming the Date
column is of type DATE
:
SELECT *
FROM `Reports`
WHERE (YEAR(`Date`) = 2010 AND MONTH(`Date`) = 9)
OR YEAR(`Date`) = 2011
general pattern
where year = 2011
or ( year = 2010 and month = 'September' )
精彩评论