mysql - 当我使用另一个表的 SUM 时无法解析列
问题描述
当我想使用Where
子句时total_debtor_price
,total_debtor_price
我收到了以下错误消息:
无法解析列
total_debtor_price
(或total_creditor_price
);
我的查询在没有where
子句的情况下工作正常。
DROP TEMPORARY TABLE IF EXISTS tmp_AccountingDocument_datatable;
CREATE TEMPORARY TABLE tmp_AccountingDocument_datatable
SELECT
TAD.*,
(
SELECT SUM(TADD.debtor_price) AS total_debtor_price
FROM Vw_AccountingDocumentDetail TADD
WHERE TADD.accounting_document_id = TAD.id
) total_debtor_price,
(
SELECT SUM(TADD.creditor_price) AS total_creditor_price
FROM Vw_AccountingDocumentDetail TADD
WHERE TADD.accounting_document_id = TAD.id
) total_creditor_price
FROM Tb_Accounting_Documents TAD
WHERE IF(
NOT ISNULL(_Filter_Price_Status),
CASE
WHEN _Filter_Price_Status = 'smaller'
THEN (
total_debtor_price <= _Filter_Price OR
total_creditor_price <= _Filter_Price
)
WHEN _Filter_Price_Status = 'equal'
THEN (
total_debtor_price = _Filter_Price OR
total_creditor_price = _Filter_Price
)
WHEN _Filter_Price_Status = 'bigger'
THEN (
total_debtor_price >= _Filter_Price OR
total_creditor_price >= _Filter_Price
)
END,
TRUE
)
解决方案
您不能在 where 子句上使用列别名,但您可以将主查询放入子查询中,以便将 where 子句与列别名一起使用
SELECT * FROM (
SELECT TAD.*,(SELECT SUM(TADD.debtor_price) as total_debtor_price FROM Vw_AccountingDocumentDetail TADD WHERE TADD.accounting_document_id = TAD.id) total_debtor_price,
(SELECT SUM(TADD.creditor_price) as total_creditor_price FROM Vw_AccountingDocumentDetail TADD WHERE TADD.accounting_document_id = TAD.id) total_creditor_price
FROM Tb_Accounting_Documents TAD
) TMP
WHERE
IF(NOT isnull(_Filter_Price_Status),
CASE
WHEN _Filter_Price_Status = 'smaller' THEN (total_debtor_price <= _Filter_Price OR total_creditor_price <= _Filter_Price)
WHEN _Filter_Price_Status = 'equal' THEN (total_debtor_price = _Filter_Price OR total_creditor_price = _Filter_Price)
WHEN _Filter_Price_Status = 'bigger' THEN (total_debtor_price >= _Filter_Price OR total_creditor_price >= _Filter_Price)
END, TRUE)
推荐阅读
- python - 为什么字典没有显示受影响的值?
- python - 如何在 QGridLayout 中动态创建 Qpushbutton?
- python - 使用 Prophet 预测倒置时间序列
- codenameone - 即使容器包含 SpanButton,如何使容器可拖动?
- mysql - 探索 json null 和 "" 以及 hibernate null 和 "" 以及 MySql/Postgres null 和 ""。有什么好的方法吗?
- javascript - 输入上的传递值索引不断变化?Vue
- java - OptimalBits/bull For Java
- node.js - 如何使用 sinon 和 chai 存根并期望调用此 aws-sdk 服务
- node.js - VS Code - 日志点不显示文件名/行号
- python - 当源是 Pandas DatafFame 时 Altair 不会绘图