我想通过使用内联 SQL 语句获取单列中的行值总和。
我使用以下命令来执行此操作。
(SELECT @fee := sum(ifnull(issued_amount, 0.00)) AS tot FROM fees_payback_fees de2
GROUP BY de2.fees_fees)
这对我来说在 phpmyadmin 中工作得很好,并且正确地输出了“tot”。
但是我在下面的 SQL 语句中使用它作为内联查询,它会出现如下错误:
“字段列表”中存在未知列“tot”
SELECT
fees_id,
locgov_id,
locgov_name,
locgov,
added_date,
month,
fee,
type,
cs,
ps,
id,
tot
FROM (
SELECT
fees_fees.fees_id, fees_locgov.locgov_id AS locgov_id,
fees_locgov.locgov_name AS locgov_name, fees_fees.locgov,
fees_fees.added_date as added_date, fees_fees.month as month, fees_fees.fee as fee,
fees_fees.type, fees_fees_verify.status AS cs, fees_fees.transfer_status AS ps,
fees_payback_fees.fees_fees AS id,
(SELECT @fee := sum(ifnull(issued_amount, 0.00)) AS tot FROM fees_payback_fees de2
GROUP BY de2.fees_fees)
from fees_fees
left join fees_fees_verify on fees_fees_verify.fees_id=fees_fees.fees_id
left join fees_locgov on fees_locgov.locgov_id=fees_fees.locgov
left join fees_payback_fees on fees_payback_fees.fees_fees=fees_fees.fees_id
where fees_fees.status=1
AND fees_fees_verify.status='Confirmed'
) tmp group by id order by fees_id DESC
可能出了什么问题?有人可以帮忙吗?
您在 tmp 派生表的选择列表中的不相关子查询中提供 tot 别名。子查询用于返回列。但是,子查询本身没有别名,如果子查询位于选择列表中,则子查询内的别名不会用作子查询外部的列别名。
您应该将 tot 别名放在子查询后面,以将其名称指示为列名:
...
(SELECT @fee := sum(ifnull(issued_amount, 0.00)) AS tot FROM fees_payback_fees de2
GROUP BY de2.fees_fees) as tot
...