2015-08-09 73 views
0

我使用創建付款收據,我會支付一定金額像例如回到同一ID組的最後一個插入行中的MySQL

下面

這已經創造了收據,但再有薪due_amount

cust_id cust_name invoice_id paid_amount due amount makeapayment 

1001  SAS    545  76   305   sasi 

這我的查詢結果。
我必須支付到期金額100然後在相同的「ID」後付費創建另一行視圖中的新行舊行並不認爲僅僅停留在數據庫中看到導致這樣

cust_id cust_name invoice_id paid_amount due amount makeapayment 

1001  SAS    545  76   205   sasi 

目前我使用的查詢

SELECT pid, cust_id, cust_name, date, invoice_id, invoice_date, invoice_amount, paid_amount, paymentmade, makeapayment, due_amount, status FROM print_reciept 
group by cust_id having due_amount!='0' ORDER BY pid DESC   

回答

0

我想你可以試試這個

SELECT pid, cust_id, cust_name, date, invoice_id, invoice_date, invoice_amount, paid_amount, paymentmade, makeapayment, due_amount, status 
FROM print_reciept AS pr 
INNER JOIN (
    SELECT MAX(pid) AS last_pid, cust_id 
    FROM print_reciept 
    GROUP BY cust_id 
) AS tmp ON (tmp.cust_id = pr.cust_id AND tmp.last_pid > pr.pid) 
GROUP BY cust_id 
ORDER BY pid DESC 
+0

不檢索任何價值完全是空的,先生給我另一個查詢 – sasi