Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select records in Mysql if the other column is set to none

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading