【发布时间】:2018-12-05 21:37:21
【问题描述】:
我尝试使用 SQL QUERY 获取特定帐号的可用余额,我的代码如下:
选择 sum(amount) 作为 Cr 表单交易,其中 credit=1 和 account_no=2549 联盟 选择 sum(amount) 作为 Dr 表单交易,其中 debit=1 和 account_no=2549
区别 Cr-Dr
【问题讨论】:
-
不清楚你想在这里做什么。
标签: mysql
我尝试使用 SQL QUERY 获取特定帐号的可用余额,我的代码如下:
选择 sum(amount) 作为 Cr 表单交易,其中 credit=1 和 account_no=2549 联盟 选择 sum(amount) 作为 Dr 表单交易,其中 debit=1 和 account_no=2549
区别 Cr-Dr
【问题讨论】:
标签: mysql
以下查询应该可以帮助您获得可用余额。
select sum(amt) from
(select
case
when credit=1 then amount
when debit=1 then -amount
else 0
end as amt
from transaction
where account_no = 5294)t;
【讨论】:
您可以使用case 在单个查询中获取贷方和借方的总和
select sum(case when credit=1 then amount else 0 end) as Cr ,
sum(case when debit=1 then amount else 0 end) Dr,
sum(case when credit=1 then amount else 0 end) - sum(case when debit=1 then amount else 0 end) as available_balance
from `transaction`
where account_no=2549
【讨论】: