我被困了很长时间而无法找到我的查询有什么问题,debit column
LEFT JOIN
出现了问题,Query Result image
显示在debit table
中,如下所示。我500
的实际值为MYSQL
,但1500
查询显示为customers table
。
我在这做错了什么。请帮助我。
这是我的cust_credit table
这是我的cust_debit table
这是我的MYSQL Query
SELECT
customers.id as id,
customers.cust_name AS customer_name,
SUM(cust_debit.debit_amount) as debit,
SUM(cust_credit.credit_amount) as credit,
(SUM(cust_debit.debit_amount)) - (SUM(cust_credit.credit_amount)) as balance
FROM customers
LEFT JOIN cust_debit ON customers.id = cust_debit.cust_id
LEFT JOIN cust_credit ON customers.id = cust_credit.cust_id
GROUP BY customers.id
ORDER BY customers.id
以下是
Query Result
我的$arrcount = ceil(count($requestarr['SelectionDetails'])/30);
$init = 0;
if (count($requestarr['SelectionDetails']) > 30) {
$endt = 29;
} else {
$endt = count($requestarr['SelectionDetails']);
}
for ($i=0; $i < $arrcount; $i++) {
$request['SelectionDetails'] = array_slice($requestarr['SelectionDetails'], $init, $endt);
$init = $endt+1;
if (count($requestarr['SelectionDetails']) > ($endt+30)) {
$endt += 30;
} else {
$endt = count($requestarr['SelectionDetails']);
}
//call to endpoind and other code here....
}
以下是
答案 0 :(得分:0)
信用表中有多行,这会在组之前产生多行,从而导致问题。如果您取出组并选择所有列,则可以看到此信息。
如果您在子联接中分组,则此问题消失,那么每个客户在分组之前都有一行。
SELECT
customers.id as id,
customers.cust_name AS customer_name,
SUM(cust_debit.debit_amount) as debit,
SUM(cust_credit.credit_amount) as credit,
(SUM(cust_debit.debit_amount)) - (SUM(cust_credit.credit_amount)) as balance
FROM customers
LEFT JOIN cust_debit ON customers.id = cust_debit.cust_id
LEFT JOIN (
SELECT cust_id, sum(credit_amount) as credit_amount)
from cust_credit
group by cust_id
) cust_credit ON customers.id = cust_credit.cust_id
GROUP BY customers.id
ORDER BY customers.id