[英]How to select column from 2nd level depth subquery?
I have found a few answers how to select a column from subquery, but it doesn't seem to be working if there is a subquery inside subquery. 我已经找到了一些如何从子查询中选择列的答案,但是如果子查询中有子查询,它似乎不起作用。
My table relationships are as follow (simplified, with example): 我的表关系如下(通过示例进行了简化):
Plans
| id | role_id
| --------------
| 4 | 2
Roles
| id | name
----------------
| 2 | Operator
Assignments
| role_name | user_id
| ---------------------
| Operator | 12
Table Plans
have foreign key role_id
to table Roles
column id
. 表
Plans
具有到表Roles
列id
外键role_id
。 And Table Roles
have foreign key id
to table Assignments
column role_name
. 并且“表
Roles
具有到表“ Assignments
列role_name
外键id
。
I'm writing a query that would select Plans
but instead of role_id
I would show user_id
from table Assignments
. 我正在写一个查询,会选择
Plans
,但不是role_id
我将显示user_id
从表中Assignments
。
I'm pretty close with my current work but I'm stuck at final step: 我目前的工作与我非常接近,但仍停留在最后一步:
SELECT assigned_user, plan.id, plan.role_id
FROM `Plans` AS plan
LEFT JOIN (
SELECT roleTable.id, roleTable.name
FROM `Roles` AS roleTable
INNER JOIN (
SELECT base_assign.user_id AS assigned_user, base_assign.role_name
FROM `Assignments` AS base_assign) AS roleAssign
ON roleTable.name = roleAssign.role_name
) AS role
ON (plan.assignee_role = role.id)
Column assigned_user
on SELECT
part (line 1) is not found but I tried by adding alias inside of subquery SELECT
. 柱
assigned_user
上SELECT
部分(第1行)中没有找到但我试图通过添加里面的子查询别名SELECT
。 How I can select base_assign.user_id
? 如何选择
base_assign.user_id
? Note that I (most likely) cannot modify database structure as it was given like this in the first place. 请注意,我(很可能)无法修改数据库结构,因为它最初是这样给出的。
You are creating subqueries too excessively and you don't even need subqueries for this. 您创建的子查询过多,甚至不需要子查询。 Subqueries can be materialized by optimzer in MySQL which can cause performance issues.
子查询可由MySQL中的optimzer实现,这可能会导致性能问题。
See https://dev.mysql.com/doc/refman/5.6/en/subquery-optimization.html 参见https://dev.mysql.com/doc/refman/5.6/en/subquery-optimization.html
Try this using simple left join
s: 使用简单的
left join
s尝试以下操作:
select a.user_id as assigned_user,
p.id,
p.role_id
from `Plans` p
left join `Roles` r on p.assignee_role = r.id
left join `Assignments` a on r.name = a.role_name;
You query wasn't working because you were not selecting the column while joining. 您的查询无效,因为您没有在加入时选择列。
Your somewhat "fixed" code: 您的“固定”代码:
select assigned_user,
plan.id,
plan.role_id
from `Plans` as plan
left join (
select roleAssign.assigned_user roleTable.id,
roleTable.name
from `Roles` as roleTable
inner join (
select base_assign.user_id as assigned_user,
base_assign.role_name
from `Assignments` as base_assign
) as roleAssign on roleTable.name = roleAssign.role_name
) as role on (plan.assignee_role = role.id)
You don't need subqueries for this. 您不需要为此的子查询。 It is a little confusing what you are trying to do.
您正在尝试做的事情有点令人困惑。 The following will return all plans, even those with no
roles
and no assignments
: 以下内容将返回所有计划,即使是没有
roles
也没有assignments
:
SELECT b.user_id as assigned_user, p.id, p.role_id
FROM Plans p LEFT JOIN
Roles r
ON p.role_id = r.id LEFT JOIN
Assignments a
ON a.name = r.name;
Subqueries are bad for two reasons. 子查询很糟糕,原因有二。 In MySQL, the are materialized.
在MySQL中,已实现。 That means extra overhead for reading and writing the tables.
这意味着读取和写入表会产生额外的开销。 Plus, you lose any indexes on them.
另外,您会丢失所有索引。
In your case, the subqueries just add layer upon layer of naming, and the names really do not help make your query more understandable. 在您的情况下,子查询只是添加了一层又一层的命名,而名称实际上并不能使您的查询更易于理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.