MySQL 相关子查询 table 名称超出范围

MySQL Correlated sub query table name out of scope

这种形式的相关子查询出现错误消息“'where clause' 中的未知列 'Invoices.TranDate'”

select InvoiceID, TranDate
    , ifnull(TotPayments,0) TotPayments, ifnull(CountPayments,0) CountPayments
from Invoices
    left join (select DebtorID, sum(TranAmount) TotPayments, count(*) CountPayments
        from CashTrans 
        where CashTrans.TranDate >= Invoices.TranDate
        group by DebtorID) PY on PY.DebtorID = Invoices.DebtorID

但是这个版本有效

select InvoiceID, TranDate
    , (select sum(TranAmount) from CashTrans 
        where CashTrans.TranDate >= Invoices.TranDate
            and CashTrans.DebtorID = Invoices.DebtorID) TotPayments
    , (select count(*) from CashTrans 
        where CashTrans.TranDate >= Invoices.TranDate
            and CashTrans.DebtorID = Invoices.DebtorID) CountPayments
from Invoices;

第一个查询有什么问题?我唯一能想到的是,在我的 Windows 系统上,我配置了 lower_case_table_names=2,因为我想保留大小写混合的名称。也许这与第一个查询在范围内没有看到 Invoice.TranDate 有关? MySQL 文档和 Internet 搜索都没有对此事有所了解。

https://dev.mysql.com/doc/refman/8.0/en/lateral-derived-tables.html 说:

A derived table cannot normally refer to (depend on) columns of preceding tables in the same FROM clause. As of MySQL 8.0.14, a derived table may be defined as a lateral derived table to specify that such references are permitted.

In SQL:1999, the query becomes legal if the derived tables are preceded by the LATERAL keyword (which means “this derived table depends on previous tables on its left side”):

我没有测试过,但我相信你的查询可以这样写:

SELECT InvoiceID, TranDate,
  IFNULL(TotPayments,0) AS TotPayments,
  ifnull(CountPayments,0) AS CountPayments
FROM Invoices
LEFT JOIN LATERAL (
  SELECT DebtorID, 
    SUM(TranAmount) AS TotPayments, 
    COUNT(*) AS CountPayments
  FROM CashTrans 
  WHERE CashTrans.TranDate >= Invoices.TranDate
  GROUP BY DebtorID
) AS PY ON PY.DebtorID = Invoices.DebtorID;

另请注意,这要求您至少使用 MySQL 8.0.14.