0
我在這裏有兩個表格。我想要的是在這兩個表格內計算(添加)數量和平衡。但困難在於交易表中的兩個account_no是相同的(A-102)。那麼如何在transactions.account_id
= account.account_no = A-102
時將amount
加到balance
?sql與另一個表格計算多個記錄
我所做的是:
select account_no, balance + (
select t.amount
from transactions t
where t.account_no = 'A-222')
from b_account
where account_no = 'A-222';
這種方法僅適用A-305和A-222。 如果我寫這樣的,它不會工作..
select account_no, balance + (
select t.amount
from transactions t
where t.account_no = (
select t.account_no
from b_account ba, transactions t
where ba.account_no = t.account_no
)
)
from b_account
where account_no = (select t.account_no
from b_account ba, transactions t
where ba.account_no = t.account_no);
任何幫助的感謝!先進
哇,它絕對可以工作。非常感謝它..但它只是輸出'A-102','A-222','A-305'這三個'account_no',如果我還想從'account'表中輸入其他記錄呢? @rs。 – Sunny 2013-03-04 18:05:32
@Sunny,檢查我更新的答案,將內部連接更改爲左外部連接,並確保當您將餘額添加到金額爲 – 2013-03-04 18:07:38
時,檢查金額是否爲零。Yh,它完全正常工作。謝謝你lotttt ..我必須做的對它進行一些研究。對我來說這是一件新事物。大聲笑。 – Sunny 2013-03-04 18:43:09