我想根據labour_master_id
來自不同表格的兩個字段的總和。但是當我得到結果時,它顯示出不同的結果。來自兩個不同表格的兩個字段的Mysql總和
這裏是我試過
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
它可以被映射兩次,它顯示錯誤的結果。
現在問題正在發生總和。
任何想法如何做到這一點?
我有這個query..please幫助錯誤
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
?你可以給數據樣本嗎? –
,因爲在第二個表可能數據退出或沒有 –
總和,如果數據退出第二個表 –