mysqli - mysql get records associated directly and indirectly -
i have 3 tables let's say
manager(id, name)
teamleader(id, name, associated)
employee(id,name,code)
in teamleader table associated foreign key of manager table.
in employee table code either id of manager or teamleader .
employee under manager teamleader.
i want write query find employee directly under manager , under teamleader associated manager.
please me solve issue.
if understood question correctly can try this:
select e.id, e.name, if(m1.id null,m1.id,m2.id) manager_id employee e left join manager m1 on m1.id = e.code left join teamleader t1 on t1.id = e.code left join manager m2 on m2.id = t1.associated ( m1.id not null or ( t1.id not null , m2.id not null ) )
if want put condition on specific manager add @ end :
having manager_id = x
x manager id wanted
Comments
Post a Comment