在 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);
关于php - 使用 Laravel 5 Eloquent 查询生成器的 DISTINCT 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30617429/