以下是我的查询,
SELECT
u.id,
CONCAT(u.FirstName, ' ', u.LastName) as customer,
(
(
CASE WHEN pmt.TotalCreditedAmt IS NOT NULL THEN pmt.TotalCreditedAmt ELSE 0 END
) - (
CASE WHEN trns.TotalDeductAmt IS NOT NULL THEN trns.TotalDeductAmt ELSE 0 END
)
) as closing_balance
FROM
(`Users` as u)
JOIN `UserAddresses` as uaddresses ON `u`.`Id` = `uaddresses`.`UserId`
AND (
uaddresses.PreferredPickUpAddress = "Yes"
)
LEFT JOIN (
SELECT
sum(CreditedAmount) AS TotalCreditedAmt,
UserId
FROM
Payment
where
PaymentSucc = "Yes"
GROUP BY
UserId
) as pmt ON `pmt`.`UserId` = `u`.`id`
LEFT JOIN (
SELECT
sum(Amount) AS TotalDeductAmt,
UserId
FROM
Transaction
GROUP BY
UserId
) as trns ON `trns`.`UserId` = `u`.`id`
WHERE
`u`.`UserType` = 'User'
AND `uaddresses`.`PincodeId` IN ('10369', '10372', '10309')
ORDER BY
`u`.`id` desc
从用户地址表条件的上面查询我的用户表记录是80
来自支付表记录中的80条记录是31,而在事务表条目中,9条是按表中的查询分组
来自两个表(付款和交易)的唯一用户34表示某些用户正在付款,而有些用户处于交易中,因此有80个用户共有34个用户。
我要求的结果是34个用户,但它给了我80个用户
任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
使用Join而不是Left join可以解决您的问题。