试着从早上开始解决这个谜团,就像我正在敲打砖墙一样。
让我给你一些背景知识。
员工将被分配不同类别的病假e-g病人等。
现在,如果员工想休假,他将提交申请,然后由他人批准。
现在我来到这里,我希望得到记录并在页面上显示,我想知道哪个员工花了多少叶子和多少叶子分配给他/她但我必须在那里显示那些叶子类别库,取决于它所分配的类别。
令人困惑所以我会放置架构截图。
这是我到目前为止所尝试的查询..但是这个查询中的问题是我将总叶数视为总体一般,我的意思是它计算所有类别叶子。在哪里应该显示员工在哪个类别中使用了多少叶
SELECT
E.employee_id,
E.full_name,
LE.no_of_leaves AS AllocatedLeaves,
MLLT.leave_type AS LeaveTypeName,
MLLT.`ml_leave_type_id` AS LeaveTypeID,
IFNULL(LA.TotalLeavesTaken,0) AS TotalLeavesTaken,
(LE.no_of_leaves - IFNULL(LA.TotalLeavesTaken,0)) AS balance
FROM employee E
INNER JOIN leave_entitlement LE ON E.employee_id = LE.employee_id AND MONTH(LE.`date_approved`) = 11
LEFT JOIN ml_leave_type MLLT ON MLLT.ml_leave_type_id = LE.ml_leave_type_id
LEFT JOIN (SELECT LA.employee_id, COUNT(1) AS TotalLeavesTaken , LA.`leave_application_id`
FROM leave_approval LA
WHERE MONTH(LA.approval_date) = 11
GROUP BY LA.employee_id
) AS LA ON E.employee_id = LA.employee_id
LEFT JOIN leave_application LAPP ON LAPP.application_id = LA.leave_application_id
LEFT JOIN ml_leave_type MLLTLA ON MLLTLA.ml_leave_type_id = LAPP.ml_leave_type_id
WHERE E.`employee_id` = 1
GROUP BY E.employee_id,LeaveTypeName;
我得到的查询结果是
如果您看到employee_id = 1
的总叶数为2
,则两个都是leaveTypeIDs,因为它在下表截图中不同
答案 0 :(得分:1)
试试这个:
SELECT
E.employee_id,
E.full_name,
LE.no_of_leaves AS AllocatedLeaves,
MLLT.leave_type AS LeaveTypeName,
MLLT.`ml_leave_type_id` AS LeaveTypeID,
IFNULL(LA.TotalLeavesTaken,0) AS TotalLeavesTaken,
(LE.no_of_leaves - IFNULL(LA.TotalLeavesTaken,0)) AS balance
FROM employee E
INNER JOIN leave_entitlement LE ON E.employee_id = LE.employee_id AND MONTH(LE.`date_approved`) = 11
LEFT JOIN ml_leave_type MLLT ON MLLT.ml_leave_type_id = LE.ml_leave_type_id
LEFT JOIN (SELECT LA.employee_id, COUNT(1) AS TotalLeavesTaken , LAPP.`ml_leave_type_id`
FROM leave_approval LA
INNER JOIN leave_application LAPP ON LAPP.application_id = LA.leave_application_id
WHERE MONTH(LA.approval_date) = 11
GROUP BY LA.employee_id, LAPP.`ml_leave_type_id`
) AS LA ON E.employee_id = LA.employee_id AND LA.`ml_leave_type_id` = LE.ml_leave_type_id
WHERE E.`employee_id` = 1
GROUP BY E.employee_id, LeaveTypeName;