我想对来自不同表 acc 的两个字段求和labour_master_id
。但是当我得到结果时,它显示出不同的结果。
这是我尝试过的
select labours.labour_master_id,
sum(labours.amount_paid),
sum(labour_cashcredits.amount_paid)
FROM labours
left join labour_cashcredits
on labours.labour_master_id=labour_cashcredits.labour_master_id
group by labours.labour_master_id
它可能映射两次,因此显示错误的结果。
现在总和出现问题。
知道怎么做吗?
我在这个查询中有错误..请帮助
select l.labour_master_id, l.amount_paid as payable, cc.amount_paid as paid
FROM
(
select labour_master_id, sum(amount_paid) amount_paid
from labours
group by labour_master_id
) l
left join
(
select labour_master_id, sum(amount_paid) amount_paid
from labour_cashcredits
group by labour_master_id
) cc
left join
(
select name,id
from labour_masters
) lm
on l.labour_master_id=cc.labour_master_id
on l.labour_master_id=lm.labour_masters.id