简体   繁体   English

mysql中的两个表,如何从这些表中获取数据

[英]Two tables in mysql, how to fetch data from those tables

I have two tables.. 我有两张桌子。

Client Bills - Contains bill infor for clients 客户账单 -包含客户账单信息

from billRecievedFromClient - contains the amount we receievd from client for a bill 来自billRecievedFromClient的 -包含我们从客户那里收到的账单金额

mysql> select * from clientBills; mysql> select * from clientBills;

+------------+------------+------------+------------+ + ------------ + ------------ + ------------ + ---------- -+

| | date | 日期| clientCode | clientCode | billNumber | billNumber | billAmount | billAmount |

+------------+------------+------------+------------+ + ------------ + ------------ + ------------ + ---------- -+

| | 2012-02-17 | 2012-02-17 | C0001 | C0001 | B0001 | B0001 | 1200.00 | 1200.00 |

| | 2012-02-17 | 2012-02-17 | C0001 | C0001 | B0002 | B0002 | 1000.00 | 1000.00 |

| | 2012-02-17 | 2012-02-17 | C0002 | C0002 | B0003 | B0003 | 1233.00 | 1233.00 |

| | 2012-02-18 | 2012-02-18 | C0003 | C0003 | B0004 | B0004 | 12000.00 | 12000.00 |

| | 2012-02-18 | 2012-02-18 | C0001 | C0001 | B0005 | B0005 | 400.00 | 400.00 |

+------------+------------+------------+------------+ + ------------ + ------------ + ------------ + ---------- -+

mysql> select * from billRecievedFromClient; mysql> select * from billRecievedFromClient;

+------------+--------------+---------------+--------+---------+ + ------------ + -------------- + --------------- + ----- --- + --------- +

| | Date | 日期| receivedCode | receiveCode | forBillNumber | forBillNumber | amount | 金额| remarks | 备注|

+------------+--------------+---------------+--------+---------+ + ------------ + -------------- + --------------- + ----- --- + --------- +

| | 2012-02-18 | 2012-02-18 | R0001 | R0001 | B0001 | B0001 | 200.00 | 200.00 | CASH | 现金

| | 2012-02-18 | 2012-02-18 | R0002 | R0002 | B0001 | B0001 | 300.00 | 300.00 | CASH | 现金

| | 2012-02-18 | 2012-02-18 | R0003 | R0003 | B0002 | B0002 | 300.00 | 300.00 | CASH | 现金

| | 2012-02-18 | 2012-02-18 | R0004 | R0004 | B0003 | B0003 | 233.00 | 233.00 | CASH | 现金

| | 2012-02-18 | 2012-02-18 | R0005 | R0005 | B0001 | B0001 | 700.00 | 700.00 | CASH | 现金

+------------+--------------+---------------+--------+---------+ + ------------ + -------------- + --------------- + ----- --- + --------- +

Now i want Information Like this --- ie for client C0001 , there are 3 bills. 现在我想要这样的信息---对于客户C0001,有3张账单。 And he made payment for one bill completelly, for one bill partially and one bill complete amount is due.. 他完全付了一张账单,一部分付了账单,而一张付完了。

+------------+------------+------------------------------------+ + ------------ + ------------ + ----------------------- ------------- +

| | billNumber | billNumber | billAmount | billAmount | SUM(billRecievedFromClient.amount) | SUM(billRecievedFromClient.amount)|

+------------+------------+------------------------------------+ + ------------ + ------------ + ----------------------- ------------- +

| | B0001 | B0001 | 1200.00 | 1200.00 | 1200.00 | 1200.00 |

| | B0002 | B0002 | 1000.00 | 1000.00 | 300.00 | 300.00 |

| | B0005 | B0005 | 400.00 | 400.00 | 00.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 JOIN 使用LEFT JOIN

SELECT *, SUM(t2.amount) FROM clientBills t1 LEFT JOIN billRecievedFromClient t2 ON t1.billNumber = t2.forBillNumber

Anyway 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 但是,如果您要为未开始的账单添加NULL记录,则可以在查询末尾添加: GROUP BY t1.billNumber

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM