I have two tables like below.
Attendance Table: emp_attendance
id,emp_id,attendance_date,status
Attendance Table: emp_leaves
id,emp_id,leave_from_date,leave_to_date,leave_type
What is the best way to join these two tables using emp_id? Incase if the emp_leaves table has more than one record for the same emp_id then the join will return two rows in the query results, this is what i have tried.
SELECT emp_attendance.emp_id, emp_attendance.attendance_date,
CASE
WHEN emp_attendance.attendance_date BETWEEN emp_leaves.leave_from_date AND emp_leaves.leave_to_date
THEN emp_leaves.leave_type ELSE emp_attendance.Status end AS final_Status
FROM emp_attendance LEFT JOIN emp_leaves
ON emp_leaves.emp_id = emp_attendance.emp_id
kindly advice.