【问题标题】:Get Available Balance from transaction table using MYSQL使用 MYSQL 从事务表中获取可用余额
【发布时间】: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


【解决方案1】:

以下查询应该可以帮助您获得可用余额。

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;

【讨论】:

    【解决方案2】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 2022-09-26
      • 2021-03-23
      • 2013-11-06
      • 2018-06-22
      相关资源
      最近更新 更多