我有两张桌子 客户表
+--------+---------+
| refno | deposit |
+--------+---------+
| 1/13 | -10 |
| 10/13 | 500 |
| 100/13 | 0 |
| 101/13 | 250 |
| 102/13 | 1000 |
+--------+---------+
Ledger Table
+--------+----------+------+----------+
| refno | quantity | rate | recieved |
+--------+----------+------+----------+
| 1/13 | 2 | 70 | 0 |
| 10/13 | 3 | 80 | 0 |
| 100/13 | 3 | 60 | 0 |
| 101/13 | 4 | 60 | 0 |
| 102/13 | 10 | 65 | 0 |
+--------+----------+------+----------+
我想在分类帐栏中添加客户列(存款)(总计) 我不想创建另一个表。 我想要
refno | total = customer.deposit+(ledger.quantity*ledger.rate-ledger.received)
1/13 | -200
10/13 | 4210
100/13| 625
101/13| 280
102/13| 1000
此致
答案 0 :(得分:0)
select c.refno, c.deposit + l.total
from customer c join ledger l on c.refno = l.refno
答案 1 :(得分:0)
这是你想要的吗?
select l.refno, l.total, c.deposit, l.total + coalesce(c.deposit, 0) as TotalWithDeposit
from ledger l left outer join
customer c
on l.refno = c.refno;
您的问题提到了聚合函数,但我没有看到任何聚合。
编辑:
select l.refno, l.total, c.deposit,
c.deposit + (l.quantity * l.rate - l.received) as TotalWithDeposit
from ledger l left outer join
customer c
on l.refno = c.refno;
答案 2 :(得分:0)
如果您在Ledger中有重复的refno,您可以尝试这种方式:
select b.refno, b.total + IFNULL(a.deposit, 0) as total
from
(select refno, sum(total) as total
from Ledger
group by refno) b
inner join Customer a on a.refno = b.refno
答案 3 :(得分:0)
我认为作者是指直接选择以外的更新,但不确定哪个表用作基本表。例如,如果我们想在Customer中将值添加到Ledger中,它应该如下所示:
更新Ledger l内部联接Cusomter c on l.refno = c.refno set l.total = l.total + c.deposit。