我正在为员工创建一个考勤系统。
有一个tbl_clockins,它包含每一个打卡/打卡会话的记录,包括每个会话是否得到支付、员工在该会话中迟到的时间或加班的时间等信息。
还有一个称为tbl_user_work_settings的表,经理可以在其中设置员工休假或因病休假的天数等。
我正在创建一些报表,其中我需要每个员工的总天数,例如在给定日期范围内每个员工休假的总天数。我有一个很长的查询,它实际上得到了所有需要的信息,但它是巨大的,而且有点低效。有没有办法使它更小/更有效?如有任何帮助,我们将不胜感激。

// get total days worked, unpaid days, bank holidays, holidays, sicknesses
// and absences within given date range for given users
$sql = "SELECT us.username, daysWorked, secondsWorked,
            unpaidDays, bankHolidays, holidays, sicknesses, absences
FROM
  (SELECT username FROM users WHERE clockin_valid='1') us
  LEFT JOIN (
    SELECT   username, selectedDate, count(isUnpaid) AS unpaidDays
    FROM     tbl_user_work_settings
    WHERE    isUnpaid = '1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) u ON us.username=u.username
  LEFT JOIN (
    SELECT   username, count(isBankHoliday) AS bankHolidays
    FROM     tbl_user_work_settings
    WHERE    isBankHoliday='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) bh ON us.username=bh.username
  LEFT JOIN (
    SELECT   username, count(isHoliday) AS holidays
    FROM     tbl_user_work_settings
    WHERE    isHoliday='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) h ON us.username=h.username
  LEFT JOIN (
    SELECT   username, count(isSickness) AS sicknesses
    FROM     tbl_user_work_settings
    WHERE    isSickness='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) s ON us.username=s.username
  LEFT JOIN (
    SELECT   username, count(isOtherAbsence) AS absences
    FROM     tbl_user_work_settings
    WHERE    isOtherAbsence='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) a ON us.username=a.username
  LEFT JOIN (
    SELECT   username, count(DISTINCT DATE(in_time)) AS daysWorked,
                SUM(seconds_duration) AS secondsWorked
    FROM     tbl_clockins
    WHERE    DATE(in_time)>='$startDate'
         AND DATE(in_time)<='$endDate'
    GROUP BY username
  ) dw ON us.username=dw.username";

if(count($selectedUsers)>0)
  $sql .= " WHERE (us.username='"
       .  implode("' OR us.username='", $selectedUsers)."')";

$sql .= " ORDER BY us.username ASC";

最佳答案

您可以在一次使用SUM(condition)表时使用tbl_user_work_settings

// get total days worked, unpaid days, bank holidays, holidays, sicknesses
// and absences within given date range for given users
$sql = "
  SELECT      users.username,
              SUM(ws.isUnpaid      ='1')       AS unpaidDays,
              SUM(ws.isBankHoliday ='1')       AS bankHolidays,
              SUM(ws.isHoliday     ='1')       AS holidays,
              SUM(ws.isSickness    ='1')       AS sicknesses,
              SUM(ws.isOtherAbsence='1')       AS absences,
              COUNT(DISTINCT DATE(cl.in_time)) AS daysWorked,
              SUM(cl.seconds_duration)         AS secondsWorked
  FROM        users
    LEFT JOIN tbl_user_work_settings           AS ws
           ON ws.username = users.username
          AND ws.selectedDate  BETWEEN '$startDate' AND '$endDate'
    LEFT JOIN tbl_clockins                     AS cl
           ON cl.username = users.username
          AND DATE(cl.in_time) BETWEEN '$startDate' AND '$endDate'
  WHERE       users.clockin_valid='1'";

if(count($selectedUsers)>0) $sql .= "
          AND users.username IN ('" . implode("','", $selectedUsers) . "')";

$sql .= "
  GROUP BY    users.username
  ORDER BY    users.username ASC";

顺便说一句(也许更多的是为了其他读者的利益),我真的希望您在将PHP变量插入SQL之前正确地转义,从而避免SQL注入攻击。理想情况下,您根本不应该这样做,而是将这样的变量作为准备好的语句的参数传递给MySQL(不会对SQL求值):阅读有关Bobby Tables的更多信息。
另外,作为旁白,为什么要将整数类型作为字符串处理(将其括在单引号字符中)?在MySQL中,执行不必要的类型转换是不必要的,也是浪费资源。事实上,如果各个isUnpaid等列都是0/1,您可以更改以上内容以删除相等测试,直接使用SUM(ws.isUnpaid)等。

09-26 03:48