MySQL,如何在 SELECT 查询中引用子查询字段
MySQL, how can I reference a subquery field in a SELECT query
我正在尝试使用以下查询
计算 MySQL 中的账户余额
SELECT accountNumber,
(
SELECT amount
FROM accountDebits
WHERE accountNumber = accounts.accountNumber
) debits,
(
SELECT amount
FROM accountCredits
WHERE accountNumber = accounts.accountNumber
) credits,
credits - debits as balance
FROM accounts
但是我得到这个错误:
Error Code: 1054. Unknown column 'credits' in 'field list'
如何在 select 中引用子查询别名?
用另一个执行计算的子查询包装它:
SELECT accountNumber, debits, credits, credits - debits as balance
FROM (
SELECT accountNumber,
(
SELECT amount
FROM accountDebits
WHERE accountNumber = accounts.accountNumber
) debits,
(
SELECT amount
FROM accountCredits
WHERE accountNumber = accounts.accountNumber
) credits
FROM accounts
) x
虽然这会启动您的查询 运行,但您最好使用外部联接:
SELECT accountNumber, debits, credits, credits - debits as balance
FROM (
SELECT
a.accountNumber,
coalesce(sum(d.amount), 0) debits,
coalesce(sum(c.amount), 0) credits,
FROM accounts a
LEFT JOIN accountDebits d
ON d.accountNumber = a.accountNumber
LEFT JOIN accountCredits c
ON c.accountNumber = a.accountNumber
) x
使用 table.field 约定,应该有效
SELECT accountNumber,
(
SELECT amount
FROM accountDebits
WHERE accountNumber = accounts.accountNumber
) debits,
(
SELECT amount
FROM accountCredits
WHERE accountNumber = accounts.accountNumber
) credits,
credits.amount - debits.amount as balance
FROM accounts
我想你想这样做:
SELECT a.*,
(COALESCE(credits, 0) - COALESCE(debits, 0)) as balance
FROM (SELECT accountNumber,
(SELECT SUM(ad.amount)
FROM accountDebits ad
WHERE ad.accountNumber = a.accountNumber
) as debits,
(SELECT SUM(ac.amount)
FROM accountCredits ac
WHERE ac.accountNumber = a.accountNumber
) credits
FROM accounts a
) a;
您需要子查询,因为您不能在定义它们的同一 SELECT
中引用派生列。您需要聚合函数将所有 debits/credits 合并为每个帐户的单个值。
我正在尝试使用以下查询
计算 MySQL 中的账户余额SELECT accountNumber,
(
SELECT amount
FROM accountDebits
WHERE accountNumber = accounts.accountNumber
) debits,
(
SELECT amount
FROM accountCredits
WHERE accountNumber = accounts.accountNumber
) credits,
credits - debits as balance
FROM accounts
但是我得到这个错误:
Error Code: 1054. Unknown column 'credits' in 'field list'
如何在 select 中引用子查询别名?
用另一个执行计算的子查询包装它:
SELECT accountNumber, debits, credits, credits - debits as balance
FROM (
SELECT accountNumber,
(
SELECT amount
FROM accountDebits
WHERE accountNumber = accounts.accountNumber
) debits,
(
SELECT amount
FROM accountCredits
WHERE accountNumber = accounts.accountNumber
) credits
FROM accounts
) x
虽然这会启动您的查询 运行,但您最好使用外部联接:
SELECT accountNumber, debits, credits, credits - debits as balance
FROM (
SELECT
a.accountNumber,
coalesce(sum(d.amount), 0) debits,
coalesce(sum(c.amount), 0) credits,
FROM accounts a
LEFT JOIN accountDebits d
ON d.accountNumber = a.accountNumber
LEFT JOIN accountCredits c
ON c.accountNumber = a.accountNumber
) x
使用 table.field 约定,应该有效
SELECT accountNumber,
(
SELECT amount
FROM accountDebits
WHERE accountNumber = accounts.accountNumber
) debits,
(
SELECT amount
FROM accountCredits
WHERE accountNumber = accounts.accountNumber
) credits,
credits.amount - debits.amount as balance
FROM accounts
我想你想这样做:
SELECT a.*,
(COALESCE(credits, 0) - COALESCE(debits, 0)) as balance
FROM (SELECT accountNumber,
(SELECT SUM(ad.amount)
FROM accountDebits ad
WHERE ad.accountNumber = a.accountNumber
) as debits,
(SELECT SUM(ac.amount)
FROM accountCredits ac
WHERE ac.accountNumber = a.accountNumber
) credits
FROM accounts a
) a;
您需要子查询,因为您不能在定义它们的同一 SELECT
中引用派生列。您需要聚合函数将所有 debits/credits 合并为每个帐户的单个值。