user2006154
user2006154

Reputation:

How to subtract seconds from current timestamp

I'm trying to create a query which will get the results that were inserted in the last 10 seconds:

SELECT *
  FROM table
  WHERE client_id = 1
    AND hash = 'x'
    AND insert_time >= current_timestamp - **10 seconds**

I've been searching how to subtract 10 seconds from the current timestamp, but I didn't succeed yet.

Upvotes: 3

Views: 16515

Answers (3)

Ed_
Ed_

Reputation: 1097

try this

TIME_TO_SEC(TIMEDIFF(current_timestamp, insert_time)) > 10;

Upvotes: 2

m.antkowicz
m.antkowicz

Reputation: 13571

Due to reference you should use SUBTIME

    ...
    and insert_time >= SUBTIME(current_timestamp,'0 0:0:10.000000');

Alternatively you can use INTERVAL as following:

    ...
    and insert_time >= SUBTIME(current_timestamp, INTERVAL 10 SECOND);

Notice that due to reference

When invoked with the INTERVAL form of the second argument, SUBDATE() is a synonym for DATE_SUB().

Upvotes: 9

VoteyDisciple
VoteyDisciple

Reputation: 37803

You Want:

insert_time >= DATE_SUB(current_timestamp, INTERVAL 10 SECOND)

Upvotes: 0

Related Questions