数据库
首页 > 数据库> > mysql – Laravel 5中的查询构建器的复杂查询

mysql – Laravel 5中的查询构建器的复杂查询

作者:互联网

我有3张桌子,

用户:

enter image description here

subscribers_lists_subscribers:

enter image description here

和subscriber_lists:

enter image description here

我想尝试这样的查询:

$baseQuery = DB::table('subscribers_lists')
    ->leftjoin('subscribers_lists_subscribers', 'subscribers_lists.id', '=', 'subscribers_lists_subscribers.subscribers_list_id')
    ->leftjoin('subscribers',  'subscribers_lists_subscribers.subscriber_id', '=', 'subscribers.id')
    ->select(
        'subscribers_lists.id as count_clickers',
        DB::raw('count(subscribers_lists_subscribers.subscribers_list_id where subscribers.status = "Active") as count_active')
        'subscribers_lists.updated_at as last_activity'
    )
    ->groupBy('subscribers_lists.id');

我需要这个:

where subscribers.status = "Active"

我收到一个错误,谁能告诉我我做错了什么?

解决方法:

我已经做了,

   $baseQuery = DB::table(  'subscribers_lists')
                            ->leftjoin('subscribers_lists_subscribers', 'subscribers_lists.id', '=', 'subscribers_lists_subscribers.subscribers_list_id')
                            ->leftjoin('subscribers',  'subscribers_lists_subscribers.subscriber_id', '=', 'subscribers.id')
                            ->select(
                                        'subscribers_lists.id',
                                        'subscribers_lists.name',
                                        'subscribers_lists.id as count_openers',
                                        'subscribers_lists.id as count_clickers',
                                        DB::raw('count(case when subscribers.status = "Active" then 1 else null end) as count_active'),
                                        DB::raw('count(case when subscribers.status = "Unsubsribed" then 1 else null end) as count_unsubsribers'),
                                        DB::raw('count(case when subscribers.status = "Bounced" then 1 else null end) as count_bounced'),
                                        DB::raw('count(subscribers_lists_subscribers.subscribers_list_id) as count_total'),
                                        'subscribers_lists.updated_at as last_activity'
                                    )
                            ->where('subscribers_lists.user_id', '=', $user_ID)
                            ->groupBy('subscribers_lists.id');
    $totalData  =   $baseQuery->count();

标签:mysql,laravel,laravel-5,query-builder
来源: https://codeday.me/bug/20190628/1314955.html