Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL如何选择表中所有相关的值?_Mysql_Sql_Tsql - Fatal编程技术网

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

MYSQL如何选择表中所有相关的值?,mysql,sql,tsql,Mysql,Sql,Tsql,我有一张如下表: 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

我有一张如下表:

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


那么我怎样才能做到这一点呢?

你研究过递归CTE的使用吗?@corky_bantam我能举个例子吗??