我将值“星期日”“星期六”“假日”“离开”“付费休假”放在名为“离开”的同一列中。但是,我将名为“WorkedDays”的列需要表格中的工作日总和。
$sqls = mysqli_query($conn, "SELECT EmployeeNoA,
EmployeeNameA,
SUM(AttenInDays),
COUNT(Leaves=='Sunday'),
COUNT(Leaves=='Saturday'),
COUNT(Leaves=='Holiday'),
COUNT(Leaves=='Leave'),
COUNT(Leaves=='Paid Leave')
FROM attend
WHERE (SignInDate BETWEEN '$StartDate' AND '$EndDate')
GROUP BY EmployeeNoA, EmployeeNameA");
我需要输出为,
-------------------------------------------------------------------------------------------
Emp No. | Emp. Name | sum(Worked Days) | count (Saturday)|count (Sunday)|count (Holiday)
-------------------------------------------------------------------------------------------
xxx001 |xxx | 20 | 4 | 4 | 2
-------------------------------------------------------------------------------------------
xxx002 |yyy | 22 | 4 | 4 | 0
-------------------------------------------------------------------------------------------
xxx003 |zzz | 21 | 4 | 4 | 1
-------------------------------------------------------------------------------------------
这些计数(星期六),计数(星期日),计数(假日)是单列,字符串值为星期六,星期日,假日,休假,带薪休假,病假等....我需要的计数使用工作日总和的单独字符串(表中为整数)
我是php的新手。所以请建议使用这些类型的查询需要什么语法。
答案 0 :(得分:0)
您需要使用SUM()
代替COUNT()
作为col = value
(另请注意,只有一个=
)返回true
或false
,1
或0
。
SELECT
EmployeeNoA,
EmployeeNameA,
SUM(AttenInDays),
SUM(Leaves='Sunday'),
SUM(Leaves='Saturday'),
SUM(Leaves='Holiday'),
SUM(Leaves='Leave'),
SUM(Leaves='Paid Leave')
FROM attend
WHERE (SignInDate BETWEEN '$StartDate' AND '$EndDate')
GROUP BY EmployeeNoA, EmployeeNameA
答案 1 :(得分:0)
检查聚合子句中的值的想法是返回一个值,如果它是真的。通常,该值为1,然后将它们相加以提供计数。
如果IF语句被明确编码为返回1或0,则会更清楚。
SELECT EmployeeNoA,
EmployeeNameA,
SUM(AttenInDays),
SUM(IF(Leaves='Sunday', 1, 0)),
SUM(IF(Leaves=Leaves='Saturday', 1, 0)),
SUM(IF(Leaves=Leaves='Holiday', 1, 0)),
SUM(IF(Leaves=Leaves='Leave', 1, 0)),
SUM(IF(Leaves=Leaves='Paid Leave', 1, 0)) ,
SUM(WorkedDays)
FROM attend
WHERE (SignInDate BETWEEN '$StartDate' AND '$EndDate')
GROUP BY EmployeeNoA, EmployeeNameA
然而,这可以缩写为仅评估为1或0的简单比较: -
SELECT EmployeeNoA,
EmployeeNameA,
SUM(AttenInDays),
SUM(Leaves='Sunday'),
SUM(Leaves='Saturday'),
SUM(Leaves='Holiday'),
SUM(Leaves='Leave'),
SUM(Leaves='Paid Leave') ,
SUM(WorkedDays)
FROM attend
WHERE (SignInDate BETWEEN '$StartDate' AND '$EndDate')
GROUP BY EmployeeNoA, EmployeeNameA