how can I get all the departments where is a ID is equal to manager column or if the manager is set to NULL it is equal to the supervisor.
table_department:
dept_id, dept_name, manager_id, supervisor_id
1 IT-admin 1 NULL
2 IT-hardware NULL 1
3 IT-system 4 1
4 Engineering 3 NULL
table_users:
user_id, username
1 username1
2 username2
3 username3
4 username4
5 username5
If in my condition the user is username1 and his id is 1, How to query and show IT-admin and IT-hardware department because username1 is the manager/supervisor of that departments.
Any help would be appreciated, Thank you.
>Solution :
Try this:
SELECT dept_id, dept_name, user_id, username
FROM table_department
INNER JOIN table_user ON COALESCE(manager_id, supervisor_id) = user_id;