[英]Subquery in FROM clause fails in MySQL
I stumbled upon a strange behavior of MySQL (v.8) when trying to run a nested subquery in the FROM clause. 当尝试在FROM子句中运行嵌套的子查询时,我偶然发现了MySQL(v.8)的奇怪行为。 The (relevant part of the) schema of the sample database I am using is as follows: 我正在使用的示例数据库的(相关部分)架构如下:
The following two queries run identically on SQL Server: 以下两个查询在SQL Server上相同地运行:
SELECT SUM(tot) as total
FROM (
SELECT
SUM(OD.quantityOrdered * OD.priceEach) as tot,
C.customerNumber
FROM customers C
INNER JOIN orders O ON C.customerNumber = O.customerNumber
INNER JOIN orderdetails OD ON O.orderNumber = OD.orderNumber
GROUP BY O.orderNumber, C.customerNumber
) AS CO
GROUP BY CO.customerNumber;
and 和
SELECT
(
SELECT SUM(tot) as total
FROM
(
SELECT
(
SELECT SUM(OD.quantityOrdered * OD.priceEach)
FROM orderdetails OD
WHERE OD.orderNumber = O.orderNumber
) AS tot
FROM orders O
WHERE O.customerNumber = C.customerNumber
) AS ORD
) AS total
FROM customers AS C;
However, on MySQL, the first one runs fine, while the second one results in an error: 但是,在MySQL上,第一个可以正常运行,而第二个会导致错误:
Error Code: 1054. Unknown column 'C.customerNumber' in 'where clause'
I will appreciate any clues about why this is happening. 我会很感激为什么发生这种情况的任何线索。 Please note that I am mostly interested not in workarounds or other ways to implement this query, but in understanding the reasons why the nested query fails. 请注意 ,我最感兴趣的不是解决此查询的方法或其他方法,而是了解嵌套查询失败的原因。
C table alias in not in scope for the suquery C表别名不在suquery的范围内
try refactoring the query using a join 尝试使用联接重构查询
eg 例如
select c.customerNumber, t.my_tot
FROM customers AS C
INNER JOIN (
SELECT O.customerNumber, SUM(OD.quantityOrdered * OD.priceEach) my_tot
FROM orderdetails OD
INNER JOIN orders O ON OD.orderNumber = O.orderNumber
GROUP BY O.customerNumber
) t on t.customerNumber = c.customerNumber
or 要么
select t.my_tot
FROM customers AS C
INNER JOIN (
SELECT O.customerNumber, SUM(OD.quantityOrdered * OD.priceEach) my_tot
FROM orderdetails OD
INNER JOIN orders O ON OD.orderNumber = O.orderNumber
GROUP BY O.customerNumber
) t on t.customerNumber = c.customerNumber
you can try like below 你可以尝试如下
SELECT
(
select SUM(tot) as total from
(
SELECT
(
SELECT SUM(OD.quantityOrdered * OD.priceEach)
FROM orderdetails OD
WHERE OD.orderNumber = O.orderNumber
) AS tot,customerNumber
FROM orders O
) as ord
WHERE ord.customerNumber = C.customerNumber
) AS total
FROM customers AS C;
Your customers table not in scope of subquery where you used in where condition WHERE O.customerNumber = C.customerNumber
so i made alias of that and then later level i used same condition where customers table has scope 您的客户表不在您在条件WHERE O.customerNumber = C.customerNumber
使用的子查询范围内,因此我WHERE O.customerNumber = C.customerNumber
了别名,然后在以后的级别中,我使用了客户表具有范围的相同条件
You have a correlated subquery in the second case. 在第二种情况下,您具有相关的子查询。 However, the correlation clause is two levels deep . 但是,相关子句有两个层次 。
Many databases will still recognize c
, even when nested multiple levels. 即使嵌套多个级别,许多数据库仍然可以识别c
。 However, MySQL (and Oracle and I think MS Access) is a database that limits correlation clauses to one level deep. 但是,MySQL(和Oracle,我认为是MS Access)是一个将关联子句限制在一个层次上的数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.