I have two tables..
Client Bills – Contains bill infor for clients
from billRecievedFromClient – contains the amount we receievd from client for a bill
mysql> select * from clientBills;
+————+————+————+————+
| date | clientCode | billNumber | billAmount |
+————+————+————+————+
| 2012-02-17 | C0001 | B0001 | 1200.00 |
| 2012-02-17 | C0001 | B0002 | 1000.00 |
| 2012-02-17 | C0002 | B0003 | 1233.00 |
| 2012-02-18 | C0003 | B0004 | 12000.00 |
| 2012-02-18 | C0001 | B0005 | 400.00 |
+————+————+————+————+
mysql> select * from billRecievedFromClient;
+————+————–+—————+——–+———+
| Date | receivedCode | forBillNumber | amount | remarks |
+————+————–+—————+——–+———+
| 2012-02-18 | R0001 | B0001 | 200.00 | CASH |
| 2012-02-18 | R0002 | B0001 | 300.00 | CASH |
| 2012-02-18 | R0003 | B0002 | 300.00 | CASH |
| 2012-02-18 | R0004 | B0003 | 233.00 | CASH |
| 2012-02-18 | R0005 | B0001 | 700.00 | CASH |
+————+————–+—————+——–+———+
Now i want Information Like this — i.e. for client C0001 , there are 3 bills. And he made payment for one bill completelly, for one bill partially and one bill complete amount is due..
+————+————+————————————+
| billNumber | billAmount | SUM(billRecievedFromClient.amount) |
+————+————+————————————+
| B0001 | 1200.00 | 1200.00 |
| B0002 | 1000.00 | 300.00 |
| B0005 | 400.00 | 00.00 |
+————+————+————————————+
remember since he not started payment for third bill, there wont be any entry for that in second table..
What can be the query for this??
please suggest..
Thanks a lot!!
Use
LEFT JOINSELECT *, SUM(t2.amount) FROM clientBills t1 LEFT JOIN billRecievedFromClient t2 ON t1.billNumber = t2.forBillNumberAnyway There won’t be record for non started bills.
But if u want NULL record for non started bills u can add at the end of query :
GROUP BY t1.billNumber