How to select mysql SUM from multiple tables and other columns from another table?

时间:2016-10-20 12:47:07

标签: php mysql sum

I have three tables: cash, cheque and bill. All three tables share two common columns: billId and customerId.

Bill Table

+--------+------------+---------+------------+
| billId | date       | bAmount | customerId |
+--------+------------+---------+------------+
| #1     | 01-05-2016 | 120.00  | 2          |
+--------+------------+---------+------------+
| #2     | 10-05-2016 | 100.00  | 2          |
+--------+------------+---------+------------+
| #3     | 20-05-2016 | 80.00   | 2          |
+--------+------------+---------+------------+
| #4     | 20-05-2016 | 70.00   | 2          |
+--------+------------+---------+------------+
| #5     | 27-05-2016 | 50.00   | 2          |
+--------+------------+---------+------------+
| #6     | 28-05-2016 | 20.00   | 2          |
+--------+------------+---------+------------+

Cheque Table

+----------+--------+------------+----------+
| chequeId | billId | customerId | chAmount |
+----------+--------+------------+----------+
| 1        | #1     | 2          | 50.00    |
+----------+--------+------------+----------+
| 2        | #2     | 2          | 25.00    |
+----------+--------+------------+----------+
| 3        | #5     | 2          | 36.00    |
+----------+--------+------------+----------+
| 4        | #4     | 2          | 23.00    |
+----------+--------+------------+----------+

Cash Table

+--------+--------+------------+----------+
| cashId | billId | customerId | caAmount |
+--------+--------+------------+----------+
| 1      | #1     | 2          | 55.00    |
+--------+--------+------------+----------+
| 2      | #2     | 2          | 70.00    |
+--------+--------+------------+----------+
| 3      | #3     | 2          | 69.00    |
+--------+--------+------------+----------+
| 4      | #4     | 2          | 23.00    |
+--------+--------+------------+----------+

I have to generate a query to generate results like below:

+--------+------------+--------+---------+
| billId | date       | amount | pending |
+--------+------------+--------+---------+
| #1     | 01-05-2016 | 120.00 | 15.00   |
+--------+------------+--------+---------+
| #2     | 10-05-2016 | 100.00 | 05.00   |
+--------+------------+--------+---------+
| #3     | 20-05-2016 | 80.00  | 11.00   |
+--------+------------+--------+---------+
| #4     | 20-05-2016 | 70.00  | 14.00   |
+--------+------------+--------+---------+
| #5     | 27-05-2016 | 50.00  | 04.00   |
+--------+------------+--------+---------+

I am sending a value for customerID to this page from another page, like $customerId = $_REQUEST['customerId'] and from this I have to select BillId and Date from the Bill Table, amount (which is computed by the sum of chAmount+caAmount), and pending (which is computed by the difference of bAmount-(chAmount+caAmount)). Since billId #6 doesn't have any records in the cheque and cash tables it doesn't need to be yielded in the results. Please mention a proper MySql query and explain it.

1 个答案:

答案 0 :(得分:1)

通常情况下,你应该尝试一些东西但是因为我在“好日子”中我写了完整的SQL语句:

SELECT b.billId, b.bDate, b.bAmount, SUM(ch.chAmount) chAmountSum, SUM(ca.caAmount) caAmountSum, (b.bAmount - ( IFNULL(SUM(ch.chAmount), 0)  + IFNULL(SUM(ca.caAmount), 0))) pending
FROM bill b
LEFT JOIN  cheque ch on ch.billId = b.billId
LEFT JOIN  cash ca on ca.billID = b.billId
GROUP BY b.billID;
  • 您必须选择要显示的所有列
  • 对于最后一个(待定),进行操作(金额 - (检查+现金)),如果它是空值,由于IFNULL SQL函数,将其替换为“0”
  • 使用LEFT JOIN,因为即使没有关联付款(支票/现金)也需要所有账单
  • 参考栏中的GROUP BY:billID