计算工作时间和选项

时间:2013-11-28 14:55:09

标签: mysql

我有一个mysql查询谁工作正常,但我需要一些选项或过滤器。 此查询计算第一个日期和结束日期之间的所有小时数。

    SELECT hour(timediff('2013-11-26 23:51:20',
                         '2013-11-28 15:51:10'))
        AS totalhour
    ----
    totalhour
    39

可以在08h到17h之间以及仅在星期一到星期五之间获得小时数的总和吗?

架构是这样的:

    CREATE TABLE `test` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `title` varchar(255) COLLATE latin1_general_ci NOT NULL,
      `first_date` date NOT NULL,
      `last_date` date NOT NULL,
      `begin_hour` time NOT NULL,
      `end_hour` time NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

    /*Data for the table `test` */

    insert  into `test`(`id`,`title`,`first_date`,`last_date`,`begin_hour`,`end_hour`) values 
    (1,'Test1','2013-11-26','2013-11-28','06:00:20','20:00:20'),
    (2,'Test2','2013-11-29','2013-12-03','09:00:10','15:51:10');
像那样 从2013-11-29在'09:00:10'到'2013-12-03'在'15:00:00'= 23 h

提前完成

1 个答案:

答案 0 :(得分:0)

是的,你可以

   SELECT hour(timediff('2013-11-26 23:51:20',
                         '2013-11-28 15:51:10'))
        AS totalhour
  ... your remaing query

WHERE  HOUR(dat_time_col) BETWEEN '8' AND '17' AND DAYNAME(dat_time_col) 
   IN('Monday','Tuesday','Wednesday','Thursday','Friday')

dat_time_col这将是您的日期时间列名称,并确保您拥有此列的正确数据类型

HOUR(date)

DAYNAME(date)

See fiddle