sql >> Base de Datos >  >> RDS >> Mysql

Usuarios multinivel en la tabla de base de datos

esto hará el trabajo - http://sqlfiddle.com/#!2/0de1f/7 :

select u1.id, u1.parent_id, u1.expected_target
from users u1
left join users u2 on u1.parent_id = u2.id
left join users u3 on u2.parent_id = u3.id
left join users u4 on u3.parent_id = u4.id
left join users u5 on u4.parent_id = u5.id
left join users u6 on u5.parent_id = u6.id
left join users u7 on u6.parent_id = u7.id
left join users u8 on u7.parent_id = u8.id
where :A_id in (u1.id, u2.id, u3.id, u4.id, u5.id,
                u6.id, u7.id, u8.id, u8.parent_id)