mysql查询:我想要2个查询结果合并w.r.t主键

时间:2014-02-13 16:58:36

标签: mysql sql

我想要两个查询结果

select refno,sum(rate*quantity-recieved) from ledger;

Ledger Table

+--------+---------------------------------------+
| refno  | sum(quantity*rate-recieved)           |
+--------+---------------------------------------+
| 1/13   | -190                                  |
| 10/13  | 3710                                  |
| 100/13 | 625                                   |
| 101/13 | 30                                    |
| 102/13 | 0                                     |
+--------+---------------------------------------+


select deposit from customer;

客户

+--------+---------+
| refno  | deposit |
+--------+---------+
| 1/13   |     -10 |
| 10/13  |     500 |
| 100/13 |       0 |
| 101/13 |     250 |
| 102/13 |    1000 |
+--------+---------+

要合并此输出;总=(customer.deposit-(ledger.rate * ledger.quantity-ledger.received))

refno | total
1/13 | -200 
10/13 | 4210
100/13| 625
101/13| 280
102/13| 1000

1 个答案:

答案 0 :(得分:0)

select ledger.refno, (sum(ledger.rate*ledger.quantity-recieved) - customer.deposit) AS total from ledger
INNER JOIN customer
ON customer.refno = ledger.refno

我认为这应该有用......