我嘗試使用下面的查詢MySQL的,我怎麼能引用一個子查詢字段在SELECT查詢
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
計算MySQL中的賬戶餘額,但我得到這個錯誤:
Error Code: 1054. Unknown column 'credits' in 'field list'
如何我可以引用select中的子查詢別名嗎?
謝謝,這工作 – adrianp
@AdrianP可以有多個信用或借記一個帳戶? – Bohemian
是的,可以有多個,我修改查詢後使用'SUM(金額)'而不是'amount' – adrianp