我有一张如下表:
AllowToView具有继承权。例如:
如果我想从RoleID 1搜索AllowToView,那么这是预期的输出:
这是理想的伪代码
Loop through the table till the end
Select AllowToView, roleID where AllowToView = RoleID
我正在考虑使用while循环或游标来做到这一点。
CREATE temporary TABLE resultTable
(
ID int(10) NOT NULL
);
insert into reusltTable select AllowToView from tbl where RoleID = 1;
insert into reusltTable select AllowToView from tbl where RoleID = (select AllowToView from tbl where RoleID = 1);
这是我当前使用的SQL,但它会导致问题:
它只选择2和3(最多2层继承),但如果表中有更多记录/更多继承,则不会显示所有记录。
那我怎么能实现这个目标呢?