简体   繁体   English

带别名的SQL嵌套查询

[英]SQL nested query with alias

Can anyone explain how this query executes and is it a Correlated Nested Query? 谁能解释该查询的执行方式,并且它是相关的嵌套查询吗?

select distinct id 
from activity ac3 
where ac3.activity_id = 151 and ac3.id in
    (select ac2.id from activity ac2 where ac2.activity_id = 150)

It is not correlated subquery. 它不是相关的子查询。 There is no link between outer query and nested query. 外部查询和嵌套查询之间没有链接。 See example of correlated subquery here . 在此处查看相关子查询的示例。

Nested subquery can be nested inside other sub queries, it is a select statement that is nested within another select statement and will return intermediate results. 嵌套子查询可以嵌套在其它子查询中,它是嵌套在另一个SELECT语句中并返回中间结果的SELECT语句。 SQL executes innermost sub query first, and then the next level. SQL执行内的次查询,然后再下一级。 The results of the sub query are the query conditions of the primary query. 子查询的结果是主查询的查询条件。

Query Execution goes as primary query -> sub query -> sub sub query and so on. 查询执行与主查询->子查询->子子查询等类似。

Correlated sub query , the sub query uses values from the outer query. 关联子查询 ,子查询使用外部查询中的值。 The sub query is evaluated once for each row processed by the outer query. 用于通过外部查询处理的每一行的子查询被计算一次。 This means that the sub query is executed repeatedly, once for each row that might be selected by the outer query. 这意味着子查询被重复执行,对于外部查询可能选择的每一行,都会重复执行一次。

This is not correlated sub query. 这是不相关的子查询。

This is not a Correlated query. 这不是一个相关查询。 If sub query is dependent on result of outermost query then it is a Correlated query. 如果子查询依赖于最外部查询的结果,则它是一个相关查询。 You need to read about it more. 您需要阅读更多内容。

This is a simple sub query. 这是一个简单的子查询。

Execution plan : select ac2.id from activity ac2 where ac2.activity_id = 150; 执行计划select ac2.id from activity ac2 where ac2.activity_id = 150;

This Query will execute first and main query is dependent on this one. 该查询将首先执行,而主查询则依赖于此查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM