我尝试用滔滔不绝的咨询,但速度很慢。 我的代码
$users = $this->user
->whereHas('friends', function($q) use($id)
{
$q->where('friend_id', '=', $id)
->where('state', '=', 'accepted');
})
->take(16)
->orderBy('name','asc')
->get();
SQL生成
select *
from `users`
where `users`.`deleted_at` is null
and
( SELECT count(*)
from `friends` as `self_04b17053094a40fb70514325b892fc19`
where `users`.`deleted_at` is null
and `self_04b17053094a40fb70514325b892fc19`.`user_id` = `users`.`id`
and `friend_id` = '1'
and `state` = 'accepted'
and `users`.`deleted_at` is null
) >= 1
order by `name` asc
limit 16