I have a MySQL table of LIKES (likeID,userID,objectID,likeDate) and I would like to be able to count all the 'likes' that have been made after the user in question.
Typically I would get the date:
SELECT likeDate FROM LIKES WHERE userID = <logged in user's ID>
and then find all dates and count the row returned (or use mysql COUNT) like this:
SELECT * FROM LIKES WHERE likeDate > <given date>
However, I'm sure there is a way to do this in one query rather than making two calls to the database. Can anyone help?
Thanks
Or as a join,
Feed the result of the first query directly into the second one:
However note that you need to add the use of
max()
in your first query.This query should be the fastest possible way to get your answer. To ensure maximum performance, add indexes on both
userID
andlikeDate
: