MySQL 8.0-13.2.11.7 Correlated Subqueries(关联子查询)

Acorrelated subqueryis a subquery that contains a reference to a table that also appears in the outer query. For example:相关子查询是一个子查询,它包含对同样出现在外部查询中的表的引用。例如:SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2 WHERE t..
摘要由CSDN通过智能技术生成

correlated subquery is a subquery that contains a reference to a table that also appears in the outer query. For example:

相关子查询是一个子查询,它包含对同样出现在外部查询中的表的引用。例如:

SELECT * FROM t1
  WHERE column1 = ANY (SELECT column1 FROM t2
                       WHERE t2.column2 = t1.column2);

Notice that the subquery contains a reference to a column of t1, even though the subquery's FROM clause does not mention a table t1. So, MySQL looks outside the subquery, and finds t1 in the outer query.

注意,子查询包含对t1列的引用,即使子查询的FROM子句没有提到表t1。MySQL查找子查询的外部,并在外部查询中找到t1。

Suppose that table t1 contains a row where column1 = 5 and column2 = 6; meanwhile, table t2 contains a row where column1 = 5 and column2 = 7. The simple expression ... WHERE column1 = ANY (SELECT column1 FROM t2) would be TRUE, but in this example, the WHERE clause within the subquery is FALSE (because (5,6) is not equal to (5,7)), so the expression as a whole is FALSE.

假设表t1包含一行,其中column1 = 5, column2 = 6;同时,表t2包含一行,其中column1 = 5和column2 = 7。这个简单的表达…WHERE column1 = ANY (SELECT column1 FROM t2)将为TRUE,但在本例中,子查询中的WHERE子句为FALSE(因为(5,6)不等于(5,7)),因此表达式作为一个整体是FALSE。

Scoping rule: MySQL evaluates from inside to outside. For example:

作用域规则:MySQL从内部到外部计算。例如:

SELECT column1 FROM t1 AS x
  WHERE x.column1 = (SELECT column1 FROM t2 AS x
    WHERE x.column1 = (SELECT column1 FROM t3
      WHERE x.column2 = t3.column1));

In this statement, x.column2 must be a column in table t2 because SELECT column1 FROM

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值