如何在Laravel 5.1中做到这一点?
SELECT *
FROM
(
SELECT subscriber_id, COUNT(*) as count
FROM mailing_group_subscriber
WHERE mailing_group_id IN ('99', '15498855416270870')
GROUP BY subscriber_id
) table_count
WHERE count = 2;
谢谢!
答案 0 :(得分:0)
您的查询未使用having,因此将全部计数并选择计数为2。因此,获取所有计数,然后通过集合选择计数2:
DB::table('mailing_group_subscriber')
->select(DB::raw('COUNT(*) as count'))
->select('subscriber_id')
->groupBy('subscriber_id')
->whereIn('mailing_group_id', ['99', '15498855416270870'])
->pluck('count', 'subscriber_id')
->where('count', 2);