I’m running this query:
SELECT COUNT(*) as num FROM items
WHERE status='public'
AND DATE(dateCreated) >= '01-01-2012'
AND DATE(dateCreated) <= '31-12-2012'
To get a count of all the items added in 2012. Result: 629
However if I change the year to 2011, I get the exact same result.
If I remove the date comparison completely, and just do:
SELECT COUNT( * ) AS num
FROM items
WHERE STATUS = 'public'
That also gives 629 rows as the result, even though there are items added all the way from 2009-2012 in the table, and each year should give a different count.
What am I doing wrong?
The dateCreated column is a datetime column.
If the column is declared as datetime, you can just compare dates using the yyyy-MM-dd format for dates or yyyy-MM-dd HH:mm:ss for datetimes.
If you use the DATE() function and compare against a date in your format, you are actually comparing strings alphabetically, so with format dd-mm-yyyy any date would start with a digit between 1 and 3.