cianz
cianz

Reputation: 159

Select where time is between -1 and 5 minutes of specified time

I want to select rows from a mySQL database where the current time is between -1 and 5 minutes relative to the 'endTime' field in the row.

The 'endTime' field is the type 'time'.

Here is my current, non working code:

SELECT  *
FROM    table
WHERE   endTime
            BETWEEN
                DATE_ADD(NOW(), INTERVAL -1 MINUTE)
            AND
                DATE_ADD(NOW(), INTERVAL 5 MINUTE)

Any help appreciated!

Upvotes: 1

Views: 2460

Answers (2)

jakeo
jakeo

Reputation: 81

Probably a little late- but this would work and be more succinct:

SELECT * FROM table WHERE endTime BETWEEN DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND NOW()

Upvotes: 0

cianz
cianz

Reputation: 159

I solved it myself:

SELECT  *
FROM    table
WHERE   endTime
            > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 MINUTE)
AND     endTime
            < DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 5 MINUTE)

Posting this in case someone else finds it useful.

Upvotes: 3

Related Questions