mysql - 怎么超过1000 :00:00 time sum using mysql

标签 mysql sql time sum

我想获得给定月份的加类总小时数,它可能超过 1000 小时。我已经有时间总和查询,但它限制了 839:59:59。如何忽略或以其他方式完成此任务。( ot_hours 存储在 VARCHAR 数据类型中)

尝试查询

SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `ot_hour` ) ) ) AS timeSum 
                 FROM groupotrequest
                 INNER JOIN department 
                 ON department.dept_Id = groupotrequest.dept_Id
                 WHERE groupotrequest.dept_Id = $dept_Id AND month LIKE '$passmonth%' AND overtime_status=6

样本数据

id  month   ot_hour dept_Id overtime_status overtime_type   date    
1   2019-10 2535:00:00  28  6   main_ot 2019-11-21 07:26:00 
2   2019-11 2535:00:00  28  6   main_ot 2019-11-21 07:27:00 
3   2019-10 20:00       28  6   sub_ot  2019-11-21 07:28:00 
4   2019-11 20:00       28  6   sub_ot  2019-11-21 07:30:00 

我想将 2019-11 的总工时数设为 2555:00:00

  function GetApprovedOt($connect,$dept_Id,$passmonth)
  {
      $query01 =$connect->prepare('SELECT SUM( TIME_TO_SEC( `ot_hour` )) 
                 FROM groupotrequest
                 INNER JOIN department 
                 ON department.dept_Id = groupotrequest.dept_Id
                 WHERE groupotrequest.dept_Id = :dept_Id AND month LIKE :passmonth% AND overtime_status=6');
      
       $query01->execute([
          ':passmonth'  => $passmonth,
          ':dept_Id'    => $dept_Id
          ]);
               
       list ($totalMins, $remngSecs) = gmp_div_qr($query01->fetchColumn(), 60);
       list ($totalHour, $remngMins) = gmp_div_qr($totalMins, 60);

       echo "Worked a total of $totalHour:$remngMins:$remngSecs.";
  } 

我也试试这个。它也不起作用

最佳答案

假设您的数据中有 2535:00:00 等值,您不能使用 time 数据类型或 time_to_sec 等函数。

一种解决方法是将字符串 2535:30:00 转换为分钟 (2535 * 60 + 30 = 152130),执行求和,然后将求和转换回小时和分钟 (152130 = 2535 小时 30 分钟):

SELECT SUM(
           SUBSTRING_INDEX(ot_hour, ':', 1) * 60 +
           SUBSTRING_INDEX(SUBSTRING_INDEX(ot_hour, ':', 2), ':', -1)
       ) div 60 AS sum_hh,
       SUM(
           SUBSTRING_INDEX(ot_hour, ':', 1) * 60 +
           SUBSTRING_INDEX(SUBSTRING_INDEX(ot_hour, ':', 2), ':', -1)
       ) mod 60 AS sum_mm
FROM t

关于mysql - 怎么超过1000 :00:00 time sum using mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58969022/

相关文章:

sql - Hive SQL Integer YYYYMM 前几个月

MySql 简单案例表达式不返回正确的数据

go - 如何使用Go语言获取当前时间(以毫秒为单位)?

mysql - Access 查询,也获取没有关系的值

MySQL:更多列与更多查询

mysql - 相当于 MySQL group_concat 的 Presto

php - 从 SELECT 中插入使列对唯一

mysql - 如何在不使用子查询的情况下获得具有 max(time) 的适当列?

python - 如何在 Python 中查看文件是否超过 3 个月?

django - 为什么 Postgres 查询比 Redis 查询快?