sql - Mysql Join three tables with a WHERE on the third -
i have 3 tables need join show following.
can join first 2 , show unique nominal_acc_no's query
select nominal_acc_no,coa_name ( select nominal_acc_no ,coa_name acc_chart_of_accounts union select nominal_acc_no,coa_name acc_chart_of_sub_accounts ) a;
but i'm struggling how join third , output results shown below. need group on nominal_acc_no , sum debit & credit on company_id in acc_posting_details table. , appreciated.
acc_chart_of_accounts coa_id nominal_acc_no 1 10 2 20 acc_chart_of_sub_accounts coa_sub_id nominal_acc_no company_id 1 10 1 2 20 1 3 110 1 acc_posting_details id nominal_acc_no debit credit company_id 1 10 25.00 1 2 10 15.00 1 3 20 30.00 1 4 110 10.00 1 5 110 8.00 1 result nominal_acc_no debit credit company_id 10 40.00 - 1 20 - 30.00 1 110 18.00 - 1
select acsa.nominal_acc_no, ifnull(sum(apd.debit),0) debit, ifnull(sum(apd.credit),0) credit, acsa.company_id acc_chart_of_sub_accounts acsa left join acc_posting_details apd on acsa.nominal_acc_no = apd.nominal_acc_no group acsa.nominal_acc_no, acsa.company_id
output
nominal_acc_no debit credit company_id ----------------------------------------------------- 10 40.00 0 1 20 0 30.00 1 110 18.00 0 1
edit : there alias problem have fixed in query. sql fiddle demo here
Comments
Post a Comment