116

I have a field of time Timestamp in my MySQL database which is mapped to a DATE datatype in my bean. Now I want a query by which I can fetch all records in the database for which the difference between the current timestamp and the one stored in the database is > 20 minutes.

How can I do it?

What i want is:

SELECT * FROM MyTab T WHERE T.runTime - now > 20 minutes

Are there any MySQL functions for this, or any way to do this in SQL?

1
  • 2
    Are you really trying to select records whose timestamp is at least 20 minutes in the future? Because that's what the condition in your query says. Commented Oct 3, 2011 at 15:03

6 Answers 6

186

If you have MySql version above 5.6 you could use TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2) something like

select * from MyTab T where
TIMESTAMPDIFF(MINUTE,T.runTime,NOW()) > 20
Sign up to request clarification or add additional context in comments.

3 Comments

select * from MyTab T where TIMESTAMPDIFF(MINUTE,T.runTime,NOW()) > 20 (T.runTime is the coulmn of MyTab table)
it doesn't include the second only show the difference of minute
My suplyed MySql doesn't have that, is version 5.5 I had to use TIMEDIFF instead.
34

MySql version >=5.6

I am using below code for today and database date.

TIMESTAMPDIFF(MINUTE,T.runTime,NOW()) > 20

According to the documentation, the first argument can be any of the following:

MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR

4 Comments

Because of the %60 you are ignoring differences larger than one hour...
@SparK is correct, remove the %60, else for 90 minutes you'll only get 30.
Not everybody has MySql 5.6 it is a new VERSION feature...
@SergioAbreu, mysql version added in answer.
28
ROUND(time_to_sec((TIMEDIFF(NOW(), "2015-06-10 20:15:00"))) / 60);

Comments

6

Try this one:

select * from MyTab T where date_add(T.runTime, INTERVAL 20 MINUTE) < NOW()

NOTE: this should work if you're using MySQL DateTime format. If you're using Unix Timestamp (integer), then it would be even easier:

select * from MyTab T where UNIX_TIMESTAMP() - T.runTime > 20*60

UNIX_TIMESTAMP() function returns you current unix timestamp.

6 Comments

I don't know is MySQL's optimizer is smart enough to do this by itself, but on general principle I'd rewrite the first condition as T.runTime < DATE_SUB(NOW(), INTERVAL 20 MINUTES).
why do you think the rewritten condition is faster than the initial one?
It might not be, if MySQL is smart enough, but in general, if you have an index on t.col, then t.col < func(const) ought to be faster than invfunc(t.col) < const because the DB can precalculate the right hand side and use the index.
@IlmariKaronen This fails for me if I use MINUTES, but works if I use MINUTE. Per the MySQL docs MINUTE is expected by DATE_ADD and DATE_SUB in ver 5.5. Does MINUTES work in other versions?
@Andrew: You're right, it should be INTERVAL 20 MINUTE. I can't edit my comment any more to fix it, but I can (and just did) edit the answer.
|
0

You can try this:
SELECT * FROM MyTab T WHERE CURRENT_TIMESTAMP() > T.runTime + INTERVAL 20 MINUTE;

The CURRENT_TIMESTAMP() is a function and returns the current date and time. This function works From MySQL 4.0

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

If you have MySql version prior than 5.6 you don't have TIMESTAMPDIFF. So,I wrote my own MySql function to do this. Accets %i or %m for minutes and %h for hours. You can extend it.

Example of usage:

 SELECT MYTSDIFF('2001-01-01 10:44:32', '2001-01-01 09:50:00', '%h')

Here goes the function. Enjoy:

 DROP FUNCTION IF EXISTS MYTSDIFF;

 DELIMITER $$
 CREATE FUNCTION `MYTSDIFF`( date1 timestamp, date2 timestamp, fmt varchar(20))
 returns varchar(20) DETERMINISTIC

 BEGIN

     declare secs smallint(2);
     declare mins smallint(2);
     declare hours int;
     declare total real default 0;
     declare str_total varchar(20);

     if date1 > DATE_ADD( date2, interval 30 day) then
       return '999999.999'; /* OUT OF RANGE TIMEDIFF */
     end if;

     select cast( time_format( timediff(date1, date2), '%s') as signed) into secs;
     select cast( time_format( timediff(date1, date2), '%i') as signed) into mins;
     select cast( time_format( timediff(date1, date2), '%H') as signed) into hours;

     set total = hours * 3600 + mins * 60 + secs;  

     set fmt = LOWER( fmt);

     if fmt = '%m' or fmt = '%i' then
       set total = total / 60;
     elseif fmt = '%h' then
       set total = total / 3600;
     else
       /* Do nothing, %s is the default: */
       set total = total + 0;
     end if;

     select cast( total as char(20)) into str_total;

     return str_total;

 END$$
 DELIMITER ;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.