[英]Using the result of query to get another result on the same table
I am struglling with this SQL query. 我对这个SQL查询很感兴趣。
The following query returns me the v2_modules.id which I then want to run again on the v2_modules table to get all the rows who has got v2_modules.id as there parentId 以下查询返回v2_modules.id,然后我想再次在v2_modules表上运行以获取所有已获得v2_modules.id的行作为parentId
select distinct v2_modules.id,v2_modules.name, v2_modules.url, v2_modules.parentId
from v2_modules
inner join v2_roles_permissions_modules
on v2_modules.id = v2_roles_permissions_modules.moduleId
inner join v2_admin_roles
on v2_roles_permissions_modules.roleId = v2_admin_roles.roleId
inner join admin
on v2_admin_roles.adminID = 89
I am struck here as to how should I get the reqired result. 我很震惊,我应该如何获得所需的结果。
You can wrap the result as a subquery, and do another join with the table: 您可以将结果包装为子查询,并对表执行另一次连接:
select *
from
(
select distinct v2_modules.id,v2_modules.name, v2_modules.url, v2_modules.parentId
from v2_modules
inner join v2_roles_permissions_modules
on v2_modules.id = v2_roles_permissions_modules.moduleId
inner join v2_admin_roles
on v2_roles_permissions_modules.roleId = v2_admin_roles.roleId
inner join admin
on v2_admin_roles.adminID = 89
)
as table1
inner join v2_modules on v2_modules.parentId = table1.id
The join condition can be reversed, I don't know if it's the one you were looking for 连接条件可以颠倒,我不知道它是否是您正在寻找的那个
like this? 像这样?
select distinct
c.id,c.name, c.url, c.parentId
from
v2_modules m inner join
(select distinct
m.id,m.name, m.url, m.parentId
from
v2_modules m inner join
v2_roles_permissions_modules p on
m.id = p.moduleId
inner join v2_admin_roles r on
p.roleId = r.roleId
full outer join admin a
where
r.adminID = 89) c on
m.parentid = c.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.