【问题标题】:Mysql calculate sum of hours worked in last 7 daysMysql计算过去7天的工作时间总和
【发布时间】:2017-07-02 04:47:44
【问题描述】:

想计算过去 7 天的总工作时间,我有一天的总工作时间。我有 4 项活动,例如学习、上学、玩耍和睡觉。我想计算过去 7 天在学习和学校上花费的总小时数。 我正在研究 Cakephp3.x 欢迎任何查询 cakephp 或 mysql

$driversHos = $this->DriverLogs->DriverLogGraphs->find('all', [
        'conditions' => [
            'OR' => [
                ['DriverLogGraphs.event_type' => 0], // On duty
                ['DriverLogGraphs.event_type' => 1] // driving
            ],
            'DriverLogGraphs.driver_id' => $id,
            'DriverLogGraphs.start_time >=' => $thisMonthFirstDay
        ],
        'contain' => [ 
            'DriverLogs.Companies',
            'DriverLogs.Driver'
        ],
        'fields' => [
            'Driver.id', 
            'Driver.username', 
            'DriverLogs.driver_id',  
            'Companies.id',
            'Companies.name', 
            'DriverLogGraphs.id',  
            'DriverLogGraphs.driver_log_id', 
            'DriverLogGraphs.event_type',  
            'event_name' => "CASE  
                WHEN DriverLogGraphs.event_type = '0' THEN 'ON DUTY' 
                WHEN DriverLogGraphs.event_type = '1' THEN 'DRIVING'  
                WHEN DriverLogGraphs.event_type = '2' THEN 'SLEEPER BIRTH' 
                WHEN DriverLogGraphs.event_type = '3' THEN 'OFF DUTY'  
            END",
            'hos_date'  => "STR_TO_DATE(DriverLogGraphs.start_time,'%Y-%m-%d')",
            'hos_start' => "CONCAT(STR_TO_DATE(DriverLogGraphs.start_time,'%Y-%m-%d'),' ','00:00:00')",
            'hos_end'   => "CONCAT(STR_TO_DATE(DriverLogGraphs.start_time,'%Y-%m-%d'),' ','23:59:59')",

            'today_start' => "CONCAT(CURDATE(), ' ', '00:00:00')",
            'today_end' => "CONCAT(CURDATE(), ' ', '23:59:59')",

            'work_time'         => 'TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time)',
            'hours_work_today'  => 'SUM(TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time))',

            's_time_7_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.start_time - INTERVAL 6 DAY,  "%Y-%m-%d" ) ,  "",  "" )',
            'e_time_7_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.end_time,  "%Y-%m-%d" ) ,  "",  "" )',
            's_time_8_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.start_time - INTERVAL 7 DAY,  "%Y-%m-%d" ) ,  "",  "" )',
            'e_time_8_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.end_time,  "%Y-%m-%d" ) ,  "",  "" )',

            'hours_work_last_7_days' => '
                SUM(
                    IF (
                        DriverLogGraphs.start_time >= CONCAT(STR_TO_DATE(DriverLogGraphs.start_time - INTERVAL 6 DAY,  "%Y-%m-%d" ) ,  " ",  "00:00:00" ) 
                        AND 
                        DriverLogGraphs.end_time <= CONCAT(STR_TO_DATE(DriverLogGraphs.end_time,  "%Y-%m-%d" ) ,  " ",  "23:59:59" ), 
                        CASE  
                            WHEN DriverLogGraphs.event_type = "0" THEN TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time) 
                            WHEN DriverLogGraphs.event_type = "1" THEN TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time)  
                            WHEN DriverLogGraphs.event_type = "2" THEN "0" 
                            WHEN DriverLogGraphs.event_type = "3" THEN "0"  
                        END, 
                        ""
                    )
                )',
        ],      
        'group' => [
            'hos_date',
        ],
        'order' => [
            'hos_start' => 'DESC', 
        ]
    ])->hydrate(false)->toArray();

Check this image for quick understanding

【问题讨论】:

标签: php mysql cakephp-3.0


【解决方案1】:

您的表中必须有一个字段日期, student_id 字段来识别学生, 考虑模型=事件

$total_worked=$this->Events->find('all', [
 'conditions'=>[
          'date >='=>DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)),
          'student_id' => $student_id
          ]
]);
$StudentWorkingHour='';
foreach($total_worked as $worked){
    $eachStudentWorkingHour=$StudentWorkingHour+($worked['study']+$worked['school'])
}

echo StudentWorkingHour; // Will result the total worked in one week

【讨论】:

  • 在我的问题上方检查我的 hours_work_last_7_days,我正在这样做
  • 我已经有了 start_time 和 end_time 和 event_type (0,1,2,3) 我只需要计算 0 和 1 event_type 的总和。浏览我的问题中附加的图片
  • 过去 7 天内 0 和 1 个 event_type 的总和。如果今天是 30,则从该月的 24 到 30 求和。如果今天是 31,那么从 31 到 25 的总和。我想我现在让你明白了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-07
  • 1970-01-01
  • 2014-08-10
  • 2014-08-07
  • 2013-01-25
  • 2012-09-10
相关资源
最近更新 更多