从表中动态查找两个给定日期之间的mysql日期差异,并计算用户计算的休假日期

时间:2016-03-16 09:37:55

标签: mysql sql date

Here is the input table

我试图找出用户采取的病叶,个人叶子等数量。所以我拿出了Leave_from&的差异。 Leave_to列被取消,差异结果加1天。同一天 - 同一天= 0 + 1天。我拿出SICK的叶子天数总和,用于个人等。

SELECT 
U.ID,   
U.FIRSTNAME,
U.LASTNAME,

L.TITLE,

ULR.LEAVE_TYPE_ID,
ULR.LEAVE_FROM,
ULR.LEAVE_TO,
DATEDIFF(LEAVE_FROM,LEAVE_TO ) AS DiffDate, -- finding date diff dynamically
SUM(DATE_ADD(DiffDate,INTERVAL 1 DAY)) AS TOTAL LEAVES -- datediff result is added by 1 day and sum of leave day is taken out 

ULR.LEAVE_DESCRIPTION, 

LST.LEAVE_STATUS



FROM USERS U

LEFT JOIN LEAVE_TYPE L
ON U.ID = L.ID

LEFT JOIN USERS_LEAVE_REQUEST ULR
ON U.ID = ULR.USER_ID

LEFT JOIN LEAVE_STATUS_TABLE LST

ON ULR.USER_ID = LST.USER_ID

1 个答案:

答案 0 :(得分:0)

    - Total Employee Leave Detail Report --

SELECT 
T1.id,
T1.firstname,
T1.lastname,
T2.leave_type_id,
T0.title,
T2.leave_from,
T2.leave_to,
DATEDIFF( T2.leave_to, T2.leave_from ) AS Date_Difference,
SUM(DATEDIFF(T2.leave_to,T2.leave_from)+1) AS Total_LeaveDays 

FROM users_leave_request T2        

INNER JOIN users T1 
ON T2.user_id = T1.id
INNER JOIN leave_type T0 
ON T2.leave_type_id = T0.id

GROUP BY
T1.id,
T1.firstname,
T1.lastname,
T2.leave_type_id,
T0.title,
T2.leave_from,
T2.leave_to,
Date_Difference