2014-02-13 195 views
0

我想兩個查詢結果MySQL查詢:我想2個查詢結果合併WRT主鍵

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

總帳表

+--------+---------------------------------------+ 
| 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 
+2

你真的有一個表,其中列名是'sum(quantity * rate-received)'? –

+1

@GordonLinoff:當你在MySQL中調用函數並且不給它別名時會發生這種情況。 –

+1

umm ...你是不是錯過了第一個查詢中的'group by'子句? – Mureinik

回答

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

我認爲這應該工作...

+0

謝謝您的回答它workeed ~~ – Naqi