$user_info = DB::table('category_user')
->select('user_id','category', DB::raw('count(*) as total'))
->groupBy('user_id','category')
->limit(3)
->orderBy('total', 'desc')
->get();
我写的代码只会显示所有类别的 3 个结果
我想限制每个类别的前 3 名的结果,例如有 10 个类别并且每个类别中有很多数据,我想只显示每个类别的前 3 名。
最佳答案
你能试试这个吗?
$user_info = DB::table('category_user')
->select('user_id','category', DB::raw('count(*) as total'))
->groupBy('user_id','category')->map(function($q){
return $q->take(3);
})->orderBy('total', 'desc')
->get();
关于sql - 如何限制laravel中每个类别的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58662834/