Self Referencing make left join not to give correct result
I have three tables in mysql database: hr_employees, appraisal_goals and hr_departments
SELECT DISTINCT e.employee_code, concat(e.first_name,' ',e.last_name) Fullname, e.email,
CASE
WHEN a.line_manager_mid_year_approved = 0 THEN "DRAFT"
WHEN a.line_manager_mid_year_approved = 1 THEN "AWAITING APPROVAL"
WHEN a.line_manager_mid_year_approved = 2 THEN "NOT APPROVED"
WHEN a.line_manager_mid_year_approved = 3 THEN "APPROVED"
ELSE "NOT STARTED"
END AS line_manager_mid_year_approved,d.dept_name,
e.grade_level_name, concat(em.first_name,' ',em.last_name) as manager
FROM hr_employees e
LEFT JOIN appraisal_goals a ON a.employee_id = e.id
INNER JOIN hr_departments d
ON e.department_id = d.id
INNER JOIN hr_employees em
ON em.employee_code = e.line_manager_id
WHERE e.company_id = 1
AND e.hr_status = 0
AND e.validation_status = 'VALID'
AND e.employee_type_code NOT IN (4,5);
-
using left join, All the employees are to be selected whether they have data in hr_departments and appraisal_goals or not.
-
Each employee have a line manager
Initially, without the self referencing (where no line manager data selected) everything was working perfectly. In order to get the manager full name, I did self referencing where em.employee_code = e.line_manager_id. As soon as I did this, the query only select the employees that have data in the appraisal_goals.
How do I correct this, so that the left join will still be working?
Thanks
Please or to participate in this conversation.