gpt4 book ai didi

php - 求和并联接多个MySQL表

转载 作者:行者123 更新时间:2023-11-29 16:34:29 25 4
gpt4 key购买 nike

美好的一天,我有三个mysql表事务,other_loan和付款。我想对表事务中的列load_amount和其他项求和,并将其与other_loan中的列数求和,并减去它在付款表中的pay_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
");


这是我的桌子:
transaction other_loan payment

我在表中显示结果,并且出现以下错误消息:


  致命错误:未被捕获的错误:在布尔值上调用成员函数fetch_assoc()


请帮忙。谢谢。

最佳答案

您的查询失败(因此$result为布尔值false),因为您两次对JOINother_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

关于php - 求和并联接多个MySQL表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com