MYSQL员工在日期范围内每天的工作时间

时间:2015-11-25 13:46:50

标签: mysql date-range days

您好我有一个MySQL查询,查看一个表,该表可以保存员工每天工作的小时数。我的查询查看日期的日期,结果是我们工作的时间。所以说2015-11-24是周二然后是8月。

如何在没有PHP的情况下运行查询以查看日期范围内的每一天? 感谢

Eg. $holidaystart = '2015-11-24';
    $holidayend = '2015-11-30';

    #Table employees hours   id   Mon   Tue   Wed   Thu  Fri  Sat  Sun
                             1    8     8     0     8    8    8    0

                $sql= "select  empId,
            '".$holidayStart."' as date,
            case dayname('".$holidayStart."')
                when 'Sunday' then Sun
                when 'Monday' then Mon
                when 'Tuesday' then Tue
                when 'Wednesday' then Wed
                when 'Thursday' then Thu
                when 'Friday' then Fri
                when 'Saturday' then Sat
            else 0 end as hours
            from employees"  ;

修改

我有一张表持有员工身份证号码(empId)然后是星期一,星期二,星期三,星期四,星期五,星期六,星期几的每一天

--------------------------------------------------------------
| empid   |  Mon  |  Tue  | Wed  |  Thu | Fri  |  Sat | Sun  |
--------------------------------------------------------------
|   1     |   8   |   8   |  0   |   8  |  8   |   8  |   0  |
|   2         0       8   |  8       0  |  8   |   8  |   8  |
--------------------------------------------------------------

所以员工1作为周一8小时,8月周二,0周三,8周四,周五8周,周六8周六工作班次。所以我想要实现的是看看2015-11-24到2015-11- 26,看看员工1在2个日期之间工作多少小时,根据他们假设每个工作小时数当天星期一= 8小时班次星期二= 8小时班次星期三= 0(休息日)星期四= 8星期五= 8星期六= 8日休息日

FIDDLE DEMO

正如您在2015-10-10演示中所看到的那样是星期六,因此1 = 8小时empId 2 = 6小时,empId 3 = 8小时,那么我如何使用日期范围进行此操作?

3 个答案:

答案 0 :(得分:0)

假设您的表格包含以下列:

empId - Employee id
workDate - the date they worked
hoursWorked - the number of hours they worked that day

然后是这样的

SELECT empId
   SUM(IF(dayname(workDate) = 'Sunday', hoursWorked, 0)) As Sun,
   SUM(IF(dayname(workDate) = 'Monday', hoursWorked, 0)) As Mon,
   SUM(IF(dayname(workDate) = 'Tuesday', hoursWorked, 0)) As Tues,
   SUM(IF(dayname(workDate) = 'Wednesday', hoursWorked, 0)) As Wed,
   SUM(IF(dayname(workDate) = 'Thursday', hoursWorked, 0)) As Thurs,
   SUM(IF(dayname(workDate) = 'Friday', hoursWorked, 0)) As Fri,
   SUM(IF(dayname(workDate) = 'Saturday', hoursWorked, 0)) As Sat,
FROM employees
GROUP BY empId
WHERE workDate BETWEEN :startDate AND :endDate

不要忘记使用准备好的语句来保护自己免受SQL注入!

答案 1 :(得分:0)

好的,所以这是一个盲目的镜头,因为你还没有为我们提供表员工的结构。尝试以下:

$sql = mysql_query('SELECT * FROM employees WHERE `date` BETWEEN ".$startDate.'" AND "'.$endDate.'"');
while($row=mysql_fetch_assoc($sql)){
    $arr[$row[date]]=$row;
}
$totalHours=0;
for($i=strtotime($startDate);$i<=strtotime($endDate);$i+=86400){
$day=$arr[date('Y-m-d H:i:s',$i];
if($day){
$id=$day[id];
}
$hours=($day)? $day[hours] : 0;
$totalHours+=$hours;
echo "Employee $id Day ".date('D',$i). " Hours $hours";
}
echo "Total hours $totalHours";

你不应该使用mysql库而是使用mysqli或PDO。

答案 2 :(得分:0)

尝试此动态数据透视查询:

SET @sql = NULL;
SELECT empId,
  GROUP_CONCAT(DISTINCT
    CONCAT(case dayname(table_date_field)
                when 'Sunday' then Sun
                when 'Monday' then Mon
                when 'Tuesday' then Tue
                when 'Wednesday' then Wed
                when 'Thursday' then Thu
                when 'Friday' then Fri
                when 'Saturday' then Sat
            else 0 end as hours
    )
  ) INTO @sql
FROM employees;
SET @sql = CONCAT('SELECT empId,', @sql, ' FROM employees GROUP BY empId');