情况:多表联合查询(三表及以上联合查询)
分析:
A left join B left join C left join D
假如:
表B、C、D都与表A关联查询
A left join B 4条数据
A left join C 2条数据
A left join D 1条数据
那么:
结果会有4条数据:B表查询数据正确;C表查询数据重复2次;D表数据重复4次
解决方案:
使用子查询(下面是laravel框架写法)
$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();
->leftJoinSub($withdraw_sum,'withdraw_sum','vt_acceptance.id','=','withdraw_sum.acceptance_id')
Enjoy it !