![](/img/trans.png)
[英]Laravel 5.3 distinct count, using eloquent instead of Query Builder
[英]DISTINCT method using Laravel 5 eloquent query builder
在laravel中使用DISTINCT方法时,我正努力获得独特的结果。 这是我的查询
//Get users that are part of this sector
$users = DB::table('users')->distinct()
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->get();
dd($users);
结果显示3个具有相同ID的用户,而我只希望其中一个用户在结果中。
我应该如何更改查询?
尝试使用ID分组
$users = DB::table('users')
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->groupBy('users.id')
->get();
dd($users);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.