【发布时间】:2015-04-13 21:45:00
【问题描述】:
我需要在我的数据库中显示每个人的运行总数,但我只能获得所有人的运行总数,所以这些是我在图片上的表格
我已经有这个查询:
SELECT
id,
studno,
if(type=0,amount,0)debit,
if(type=1,amount,0)credit,
if(type=0,@bal:=@bal+amount,@bal:=@bal-amount) runningTotal
FROM
(SELECT id, studno, amount, 0 type from tblPayables
UNION ALL
SELECT id, studno, amount, 1 type from tblPayments)s, (SELECT @bal:=0)b
ORDER BY studno, id, type;
但问题是我只能得出这个结果:
突出显示的数字应为 50,因为它用于不同的 studno
【问题讨论】:
-
为什么贷方 5k 在借方 10k 之后和借方 50 之前应用?你用 id 3 添加新的信用是什么?结果应该是什么样子?
-
这只是要求之一,因为我需要交替显示借方和贷方
-
问题是我需要每个 studno 都有自己的变量@bal
-
我们不能,因为#3 应该有一些东西要先支付或借记
-
我们可以,stud2 支付 50 - 这是 id 2,然后 stud 1 支付 50 - 这是 id 3
标签: mysql sql database cumulative-sum