benhowdle89
benhowdle89

Reputation: 37504

Mysql total elapsed times from two dates

If i have two rows

id created          start            stop
1  28-01-2011 23:00 28-01-2011 23:00 28-01-2011 23:01  
2  28-01-2011 23:10 28-01-2011 23:10 28-01-2011 23:11

What query can i run to get the total elapsed time for one date, so for 28-01-2011 the total time was 2 minutes.

Is this possible?

Upvotes: 2

Views: 272

Answers (1)

jancha
jancha

Reputation: 4977

well one option is straight forward:

select sum(unix_timestamp(stop) - 
    unix_timestamp(start))/60 from table
    where date(created) = '28-01-2011';

mysql> select now(), date_add(now(), interval 10 minute);
+---------------------+-------------------------------------+
| now()               | date_add(now(), interval 10 minute) |
+---------------------+-------------------------------------+
| 2011-10-04 13:29:56 | 2011-10-04 13:39:56                 |
+---------------------+-------------------------------------+
1 row in set (0.01 sec)

mysql> select sum(unix_timestamp(now()) -
          unix_timestamp(date_add(now(), interval 10 minute)));
+----------------------------------------------------------------------------------+
| sum(unix_timestamp(now()) - unix_timestamp(date_add(now(), interval 10 minute))) |
+----------------------------------------------------------------------------------+
|                                                                             -600 |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> select sum(unix_timestamp(now()) - 
          unix_timestamp(date_add(now(), interval 10 minute))) / 60 ;
+---------------------------------------------------------------------------------------+
| sum(unix_timestamp(now()) - unix_timestamp(date_add(now(), interval 10 minute))) / 60 |
+---------------------------------------------------------------------------------------+
|                                                                              -10.0000 |
+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Upvotes: 1

Related Questions