php + mysql database right side of the left join a union query data duplication

: The multi-table joint inquiry (three tables and more joint inquiry)

analysis:

A left join B left join C left join D

if:

TABLE B, C, D are associated with the query table A

A left join B 4 pieces of data 

A left join C 2 pieces of data

A left join D 1 pieces of data

Then:

Results have four data: B table query data correctly; C was repeated twice to query the data tables; data in Table D is repeated four times

solution:

Use sub-queries (The following is laravel framework written)

$withdraw_sum=Acceptance::query()->from('vt_withdraw_master_order')->where('admin_approval_status',config('constant.WITHDRAW_APPROVAL_STATUS.approved'))->groupBy('acceptance_id')->select('acceptance_id',DB::raw('sum(order_amount) as given_money'))->getQuery();
View Code
->leftJoinSub($withdraw_sum,'withdraw_sum','vt_acceptance.id','=','withdraw_sum.acceptance_id')
View Code

 

Guess you like

Origin www.cnblogs.com/daizhongxing/p/11458504.html