I have a table that contains the next columns:
ip(varchar 255), index(bigint 20), time(timestamp)
each time something is inserted there, the time column gets current timestamp.
I want to run a query that returns all the rows that have been added in the last 24 hours.
This is what I try to execute:
SELECT ip, index FROM users WHERE ip = 'some ip' AND TIMESTAMPDIFF(HOURS,time,NOW()) < 24
And it doesn’t work.
HOURS should be HOUR. See the documentantion for TIMESTAMPADD to see the valid values of the
unitparameter:So your query should be:
Note also that this query won’t be able to take advantage of an index on time, if one exists. It could be more efficient to rewrite the query as follows: