这是我PHP Sum a value in while loop, but with conditions
的问题的延续我有两个表要加入,1个是用户,1个是出席。 表格出勤时的列isOt表示用户被上级授予加班费。
TABLE : attendance
++++++++++++++++++++++++++++++
id userId totalHours isOt dateRecorded
1 1 0745 0 02-06-2014
2 3 0845 1 07-06-2014
3 1 0945 1 12-06-2014
TABLE : user
+++++++++++++++++++++++
id name departmentId
1 John 2
2 Sean 2
3 Allan 2
在很多人的帮助下,提供此查询的解决方案确实帮助我为每个用户提供SUM totalHours
但是我做了一些额外的查询行,如下所示:
$query = "SELECT u.employeeName, u.id, a.isOt, a.dateRecorded,
SUM((CAST(totalHours AS UNSIGNED) % 100)/60 + FLOOR(CAST(totalHours AS UNSIGNED)/100)) grandTotal FROM user u RIGHT JOIN attendance a
ON u.id = a.userId
WHERE u.departmentId = 2
GROUP BY u.id HAVING dateRecorded >= '01-06-2014' <= '31-06-2014'";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result))
{
print $row['id'] . ' ' . $row['grandTotal'];
}
上面的查询成功地显示了两行输出,就像我想要的那样 但是现在对于像我这样的初学者来说还有另一个棘手的挑战
我需要通过发言来获得加时赛的总时间
(if isOt == 1)
{
minus totalHours with 0800 (min working time)
and them sum up the balance and echo it for each user
}
任何帮助表示赞赏,我仍然是初学者并且尽力学习。 提前谢谢你:)
答案 0 :(得分:0)
我会在陈述
时使用一个案例SELECT u.employeeName,
u.id,
a.isOt,
a.dateRecorded,
Case When a.totalHours > 0800 Then 0800 Else totalHours End As NormalHours
Case When a.totalHours > 0800 Then totalHours - 0800 Else 0 End As OTHours
FROM user u RIGHT JOIN
attendance a
ON u.id = a.userId
WHERE u.departmentId = 2
GROUP BY u.id
HAVING dateRecorded >= '01-06-2014' <= '31-06-2014'
假设totalHours以数字格式存储。