13

私はこの結合を持っています:

Return DB::table('volunteer')
            ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
            ->select(array('*','volunteer.id AS link_id'))
            ->where('is_published', '=', 1)

しかし、当然のことながら重複したレコードが返されるので、次を使用しようとしますdistinct()

Return DB::table('volunteer')
            ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
            ->select(array('*','volunteer.id AS link_id'))
                        ->distinct()
            ->where('is_published', '=', 1)

しかし、SQLで簡単に実行できるdistinct() 特定の単一フィールドで使用したいと考えています。パラメータを取らないようです。distinct()つまり、言えませんdistinct('volunteer.id')

重複したレコードを削除する方法を教えてもらえますか? これは私にとって別の額のスラッパーだと思います.

4

1 に答える 1

26

私のプロジェクトでは、私も試しdistinct()てみgroupby()ましたが、両方ともうまくいきました:

//Distinct version.
Company_Customer_Product::where('Company_id', '=', $companyid)->distinct()->get(array('Customer_id'));
//Goup by version.
Company_Customer_Product::where('Company_id', '=', $companyid)->groupby('Customer_id')->get(array('Customer_id'));

これによるとdistinct()、あなたのケースでも機能するはずです。次のように使用してget()ください:

Return DB::table('volunteer')
   ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
   ->select(array('*','volunteer.id AS link_id'))
   ->distinct()
   ->where('is_published', '=', 1)
   ->get(array('volunteer.id'));

distinct()それ以外の場合は、使用するときに必要ないため、次を使用groupby()できます。

Return DB::table('volunteer')
   ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id')
   ->select(array('*','volunteer.id AS link_id'))
   ->group_by('volunteer.id')
   ->where('is_published', '=', 1)
   ->get(array('volunteer.id'));
于 2013-08-12T11:32:27.067 に答える