简体   繁体   English

使用Laravel 5雄辩的查询生成器的DISTINCT方法

[英]DISTINCT method using Laravel 5 eloquent query builder

I'm struggling to get unique results when using the DISTINCT method in laravel. 在laravel中使用DISTINCT方法时,我正努力获得独特的结果。 This is my query 这是我的查询

//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);

The result shows 3 users with the same ID, when I only want one of them in the results. 结果显示3个具有相同ID的用户,而我只希望其中一个用户在结果中。

How should I change my query? 我应该如何更改查询?

Try to use group by 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM