在laravel中加入where子句

时间:2020-04-02 11:56:01

标签: laravel

我正在查询以计算5个制表符计数的唯一总和。
当前逻辑是

项目计数(308)=计数(tab1 + tab2 + tab3 + tab4 + tab5)

但是需要将逻辑更改为:显示所有5个选项卡中唯一项目的计数。由于一个选项卡中的项目也可以位于其他选项卡中。

enter image description here

我现在将所有5个选项卡的sql代码组合到一个查询中,以查找唯一项目总数。 合并前三个选项卡并不是什么大问题,因为它具有要联接的公用表,但是对于第4个和第5个选项卡,它正在与其他表联接以查找计数。
现在,我认为第4个和第5个选项卡的联接查询根本不起作用。

这是用于查询第4个标签页计数的查询。

$countComplete = DB::table('t_project as pro')
        ->leftjoin('t_project_cellphone as co', 'pro.id','=','co.project_id')
        ->leftjoin('t_contract_cellphone as tdic','pro.id','=','tdic.project_id')
        ->whereIn('pro.status', [9,10,11,12,13,14,15,16,17,18,22])
        ->where('pro.revisit_status',0)
       // ->where('co.cancel_user',  0)
        ->where('co.s_display_4', 0)
        ->whereNotNull('tdic.reg_completed_at')
        ->groupby('pro.id')
        ->count();

这里是用于查找唯一项目总数的查询。

$projectcount = DB::table('t_project as pro')
                    ->leftjoin('t_project_cellphone as co', 'pro.id','=','co.project_id')
                    // ->leftjoin('t_contract_cellphone as tdic','pro.id','=','tdic.project_id')
                    // ->leftjoin('t_project_completed_cellphone as tcc', 'pro.id','=', 'tcc.project_id')
                    ->where(function($join){
                        $join
                        ->where(function($join1){
                            $join1->whereIn('pro.status', [9,10,11,12,13,14,15,16,17,18,22,CellPhoneStatus::DISAPPROVAL])
                                ->where('pro.product_id', 1)
                                ->where('co.s_display_1', 0);
                        })
                        ->orWhere(function($join2){
                            $join2->whereIn('pro.status', [9,10,11,12,13,14,15,16,17,18,22])
                                ->where('co.s_display_2', 0)
                                ->where('co.biz_status','=',1);

                        })
                        ->orWhere(function($join3){
                            $join3->whereIn('pro.status', [9,10,11,12,13,14,15,16,17,18,22])
                                ->where('co.s_display_3', 0)
                                ->where('pro.revisit_status',0)
                                ->where(function($join31){
                                    $join31->where('co.ship_status',1)
                                        ->orwhere('co.ship_battery_status',1);
                                });
                        })
                        ->orWhere(function($join4){
                            $join4
                            // ->leftjoin('t_contract_cellphone as tdic','pro.id','=','tdic.project_id')
                                ->leftjoin('t_contract_cellphone as tdic', function($join41){
                                    $join41->on('pro.id','=','tdic.project_id')
                                    ->whereNotNull('tdic.reg_completed_at');
                                })
                                 ->whereIn('pro.status', [9,10,11,12,13,14,15,16,17,18,22])
                                ->groupby('pro.id')
                                ->where('co.s_display_4', 0);

                        })
                        ->orWhere(function($join5){
                            $join5
                                // ->leftjoin('t_project_completed_cellphone as tcc', 'pro.id','=', '.project_id')
                                ->leftjoin('t_project_completed_cellphone as tcc', function($join51){
                                    $join51->on('pro.id','=','tcc.project_id')
                                    ->where('tcc.total_complete_status', 1);
                                })
                                ->whereIn('pro.status', [9,10,11,12,13,14,15,16,17,18,22])
                                ->where('co.s_display_5', 0);
                        })
                        ;
                    })
                    ->where('pro.revisit_status',0)
                    ->count();  

有人可以帮我解决加入该问题的问题吗?

0 个答案:

没有答案