sum和连接多个mysql表

9rbhqvlz  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(387)

你好,我有三张mysql表,包括交易、其他贷款和付款。我想对load\u amount和additional from table transaction列求和,然后将其和other\u loan列amount相加,再减去payment table中payment\u amount列的和。下面是我的代码:

$result = $db->query("
SELECT *
     , (sum(load_amount) + sum(additional) + sum(amount)) - sum(payment) as total 
  FROM transaction
     , other_loan
     , payment
  INNER JOIN client 
    ON client.id=transaction.id 
  INNER JOIN other_loan 
    ON other_loan.id=transaction.id 
  INNER JOIN payment 
    ON payment.id=transaction.id 
 INNER GROUP 
    BY client.id
  ");

这是我的table:交易其他贷款付款
我正在表格中显示结果,并显示以下错误消息:
致命错误:未捕获错误:对布尔值上的成员函数fetch\u assoc()调用
请帮忙。谢谢您。

j2qf4p5b

j2qf4p5b1#

您的查询失败(因此 $result 是布尔型的 false )因为你有 JOIN ed表格 other_loan 以及 payment 两次(第一次使用隐式 JOIN 操作员 , )不给他们取别名。我怀疑您实际上不想这样做,您的查询应该如下所示:

$result = $db->query("SELECT *, (SUM(load_amount) + SUM(additional) + SUM(amount)) - SUM(payment) as total 
                      FROM transaction
                      INNER JOIN client ON client.id=transaction.id 
                      INNER JOIN other_loan ON other_loan.id=transaction.id 
                      INNER JOIN payment ON payment.id=transaction.id
                      GROUP BY client.id");

我也怀疑 load_amount 实际上应该是 loan_amount .

相关问题