jcobhams
jcobhams

Reputation: 824

Select from table where timestamp is within x seconds

Am in some kind of mysql logic mess. I want to select rows from a table where the row timestamp is within the last 15 seconds of the current time.

More detail: timestamp field is generated using php time() function, am tryin to select if the timestamp is 15seconds or younger compared to the current timestamp

What I have tried:

SELECT * FROM notifs WHERE `foo` = 'bar' AND `timestamp` > TIME(DATE_SUB(NOW(), INTERVAL 15 SECOND))");

and

SELECT * FROM notifs WHERE `foo` = 'bar' AND `timestamp` < unix_timestamp(NOW() - INTERVAL 15 SECOND)

They both return false positives. They give me rows i know are older than 15seconds ago. Please what could i be missing?

Upvotes: 1

Views: 478

Answers (1)

peterm
peterm

Reputation: 92805

Try it this way

SELECT *
  FROM notifs
 WHERE foo = 'bar'
   AND timestamp BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 15 SECOND) 
                     AND UNIX_TIMESTAMP(NOW())

Upvotes: 1

Related Questions