Calculate the working time : DATETIME « Date Time « SQL / MySQL






Calculate the working time

     
mysql>
mysql> CREATE TABLE mytable (
    ->     id INT AUTO_INCREMENT,
    ->     begintime DATETIME,
    ->     endtime DATETIME,
    ->     PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO mytable (begintime, endtime) VALUES
    ->         ('2005-03-27 7:15', '2005-03-27 18:00'),
    ->         ('2005-03-28 8:00', '2005-03-28 18:00'),
    ->         ('2005-03-29 7:30', '2005-03-29 16:50'),
    ->         ('2005-03-30 7:00', '2005-03-30 17:15');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql>
mysql> SELECT DATE_FORMAT(begintime, '%Y-%m-%d') AS dt,
    -> endtime - begintime AS s
    -> FROM mytable;
+------------+---------------+
| dt         | s             |
+------------+---------------+
| 2005-03-27 | 108500.000000 |
| 2005-03-28 | 100000.000000 |
| 2005-03-29 |  92000.000000 |
| 2005-03-30 | 101500.000000 |
+------------+---------------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
  








Related examples in the same category

1.Add times with SUM.
2.Display DATETIME values from the datetime_val table using formats that include the time of day