我想计算每个雇员的休假时间。
我有以下表格:
有(id,empid,check_in,check_out,date)列的出勤
保留(id,empid,reason,time_long from_date,to_date)列
具有(id,name,….)列的员工
这是我的问题:

select `emp`.*, `p`.*, `a`.*, `l`.`id` as `leaveId`, `l`.`time_long` as `leaveLong`, `l`.`from_date` as `leaveFrom`, `l`.`to_date` as `leaveTo` from `employee` as `emp` inner join `attendance` as `a` on `emp`.`id` = `a`.`empid`  left join `leave` as `l` on `emp`.`id` = `l`.`empid` where `a`.`date` between 2019-03-01 and 2019-03-31 order by `emp`.`id` asc)

查询返回以下记录。
[
   {
      "log_id": 1310,
      "name": "ahmad",
      "empid": 3,
      "check_in": "11:56",
      "check_out": "17:25",
      "date": "2019-03-23",
      "time_long": "5:28",
      "leaveId": 5,
      "leaveLong": 16,
      "leaveFrom": "2019-03-15",
      "leaveTo": "2019-03-17"
    },
    {
      "log_id": 1311,
      "name": "ahmad",
      "empid": 3,
      "check_in": "07:14",
      "check_out": "17:24",
      "date": "2019-03-24",
      "time_long": "10:9",
      "leaveId": 5,
      "leaveLong": 16,
      "leaveFrom": "2019-03-15",
      "leaveTo": "2019-03-17"
    },{
      "log_id": 1312,
      "name": "ahmad",
      "empid": 3,
      "check_in": "06:58",
      "check_out": "17:21",
      "date": "2019-03-25",
      "time_long": "10:23"
      "leaveId": 5,
      "leaveLong": 16,
      "leaveFrom": "2019-03-15",
      "leaveTo": "2019-03-17"
    },{
      "log_id": 1313,
      "name": "ahmad",
      "empid": 3,
      "check_in": "07:58",
      "check_out": "17:21",
      "date": "2019-03-26",
      "time_long": "9:23"
      "leaveId": 15,
      "leaveLong": 8.0,
      "leaveFrom": "2019-03-28",
      "leaveTo": "2019-03-29"
    },
    {
      "log_id": 1314,
      "name": "ahmad",
      "empid": 3,
      "check_in": "07:58",
      "check_out": "17:21",
      "date": "2019-03-26",
      "time_long": "9:23"
      "leaveId": 15,
      "leaveLong": 8.0,
      "leaveFrom": "2019-03-28",
      "leaveTo": "2019-03-29"
    },
    {
      "log_id": 1315,
      "name": "ahmad",
      "empid": 3,
      "check_in": "08:00",
      "check_out": "16:00",
      "date": "2019-03-27",
      "time_long": "8:00"
      "leaveId": 15,
      "leaveLong": 8.0,
      "leaveFrom": "2019-03-28",
      "leaveTo": "2019-03-29"
    }
    { ... }
  ]

因此,我期望从这个输出中得到以下结果:
ID为3的员工休假=24小时

最佳答案

在这种情况下,可以使用FoeCH用EMPID分组数据,

$temp = [];
foreach ($arr as $key => $value) {
    // fetching all data as per empid and leave id
    $temp[$value['empid']][$value['leaveId']][] = $value['leaveLong'];
}
$result = [];
foreach ($temp as $key => $value) {
    foreach ($value as $key1 => $value1) {
        // fetching max value for empid and leave id
        $result[$key][$key1] = max($value1);
    }
}
// summing per emp id
$result = array_map("array_sum", $result);
print_r($result);

Demo
产量
Array
(
    [3] => 24
    [8] => 0
)

08-03 12:19