MYSQL如何选择表中的所有相关值?

时间:2016-03-08 03:37:32

标签: mysql sql tsql

我有一张如下表:

enter image description here

AllowToView具有继承权。例如:

如果我想从RoleID 1搜索AllowToView,那么这是预期的输出:

enter image description here

这是理想的伪代码

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层继承),但如果表中有更多记录/更多继承,则不会显示所有记录。

那我怎么能实现这个目标呢?

0 个答案:

没有答案