I’m sure this is really simple… just tired now.
I have a query
SELECT post.*, votes.datetime, COUNT(votes.post_id) AS votes
FROM posts LEFT JOIN votes
ON posts.id = votes.post_id
GROUP BY posts.id
ORDER BY votes DESC
I need to fetch all posts on the left table whilst when counting votes on the votes table using the votes which happened today or between 1 hour and another hour of the same day. Does this make enough sense?
I have tried adding
WHERE DATE(votes.datetime) = DATE(NOW())
But no luck, it just shows the rows which have votes which happened today and no other posts rows which have either no votes at all or votes that happened today.
I am using data type datetime for the datetime column
Any ideas how I can do this easily?
Thanks in advance!
-Stefan
Use a subquery in your LEFT JOIN votes expression, specifying the condition you seek as a WHERE clause within this. Then later, if necessary, optimise for performance by transforming away the subquery.