duane2364 2016-10-20 12:47
浏览 44
已采纳

如何从多个表和另一个表中的其他列中选择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条回答 默认 最新

  • duanniying2342 2016-10-20 13:58
    关注

    Normally, you should try something but as I am in my "good days" I wrote the full SQL statement :

    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;
    
    • You have to select all columns that you want to display
    • For the last one (pending), make your operation (amount - (cheque + cash)) and if it is a null value, replace it by "0" thanks to IFNULL SQL function
    • Use LEFT JOIN because you want all bills even if there is no associated payment yet (cheque / cash)
    • GROUP BY from your reference column : billID
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题