from子句中的子查询在mysql中失败

jxct1oxe  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(363)

在尝试运行from子句中的嵌套子查询时,我偶然发现了mysql(v.8)的一个奇怪行为。我使用的示例数据库的(相关部分)架构如下所示:

以下两个查询在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;

以及

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;

但是,在mysql上,第一个运行正常,而第二个会导致错误:

Error Code: 1054. Unknown column 'C.customerNumber' in 'where clause'

如果有任何线索能告诉我为什么会这样,我会很感激的。请注意,我最感兴趣的不是解决方法或实现此查询的其他方法,而是理解嵌套查询失败的原因。

xlpyo6sf

xlpyo6sf1#

在第二种情况下,您有一个相关的子查询。然而,关联从句有两个层次。
许多数据库仍能识别 c ,即使嵌套了多个级别。然而,mysql(以及oracle和我认为msaccess)是一个数据库,它将相关子句限制在一个层次的深度。

wgx48brx

wgx48brx2#

你可以像下面这样试试

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;

您的customers表不在您在where条件中使用的子查询范围内 WHERE O.customerNumber = C.customerNumber 所以我做了一个别名,然后在更高的级别我使用了customers表具有作用域的相同条件

ep6jt1vc

ep6jt1vc3#

c表别名不在suquery的作用域中
尝试使用连接重构查询

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

或者

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

相关问题