Mysql – select rows older then column DATETIME – INT value of other column in single query

datetimefunctionsMySQL

I have 2 columns in my table:

(DATETIME)lastsendattempt
(INT)sendgap

I know I can select all rows that have a "lastsendattempt" DATETIME which is older then the current time. But, am I able to use the value of another column to select the rows that are older than the current time – that sendgap value ?

I want it all in a single query instead of getting the sendgap value in a query and then doing another one with a datetime value

Something like…

SELECT * FROM table WHERE lastsendattempt <= (2012-04-10 01:30:15)-(INT)sendgap

Best Answer

If sendgap is the number of seconds, the correct syntax is this

SELECT * FROM table WHERE lastsendattempt <= (NOW() - INTERVAL sendgap SECOND);