Count rows per date in a date range
I have been searching Google and SO for the last 25 min searching for how
to do the following in MySQL.
I currently have the following query (Sent by PHP):
SELECT
COUNT(*),
`$db`.`crop`.`id` AS `crop`,
`$db`.`crop`.`harvest_date` AS `harvest_date`
FROM
`$db`.`crop`
WHERE
`$db`.`crop`.`harvest_date` BETWEEN $startDate AND $endDate
GROUP BY `$db`.`crop`.`harvest_date`
$startDate = 2012-01-01
$endDate = 2013-07-01
I am trying to find all the rows that have a harvest_date between start
and end dates, and then count the number of rows that fall on the same
date. However, I seem to be getting no results. The query doesn't fail, it
just doesn't return anything. Can anyone point me in the right
direction/tell me where I got it wrong?
EDIT: Found the problem. As Michael pointed out below, the dates were not
getting passed as dates, but as numbers. I solved this by adding ' before
and after startDate and endDate in the query.
No comments:
Post a Comment