我有3张桌子,
subscribers
:
subscribers_lists_subscribers
:
subscribers_lists
:
我正在尝试这样的查询:
$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"
我收到错误,有人能告诉我我做错了吗?
答案 0 :(得分:3)
我已经完成了,
$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();
答案 1 :(得分:0)
在其中一个选择之后,您缺少一个逗号,并且您不能简单地在select语句中添加“where subscriber.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 as count_clickers',
DB::raw('COUNT(subscribers_lists_subscribers.subscribers_list_id) as count_active'),
'subscribers_lists.updated_at as last_activity'
)
->where('subscribers.status', 'Active')
->groupBy('subscribers_lists.id');