Laravel Eloquent - distinct() and count() not working properly together

前端 未结 12 1103
甜味超标
甜味超标 2020-12-04 15:54

So I\'m trying to get the number of distinct pids on a query, but the returned value is wrong.

This is what I try to do:

$ad->getcodes()->group         


        
相关标签:
12条回答
  • 2020-12-04 16:33

    try this

    $ad->getcodes()->groupby('pid')->distinct()->count('pid')
    
    0 讨论(0)
  • 2020-12-04 16:34

    The following should work

    $ad->getcodes()->distinct('pid')->count('pid');
    
    0 讨论(0)
  • 2020-12-04 16:35
    $solution = $query->distinct()
                ->groupBy
                (
                    [
                        'array',
                        'of',
                        'columns',
                    ]
                )
                ->addSelect(
                    [
                        'columns',
                        'from',
                        'the',
                        'groupby',
                    ]
                )
                ->get();
    

    Remember the group by is optional,this should work in most cases when you want a count group by to exclude duplicated select values, the addSelect is a querybuilder instance method.

    0 讨论(0)
  • 2020-12-04 16:36

    Anyone else come across this post, and not finding the other suggestions to work?

    Depending on the specific query, a different approach may be needed. In my case, I needed either count the results of a GROUP BY, e.g.

    SELECT COUNT(*) FROM (SELECT * FROM a GROUP BY b)
    

    or use COUNT(DISTINCT b):

    SELECT COUNT(DISTINCT b) FROM a
    

    After some puzzling around, I realised there was no built-in Laravel function for either of these. So the simplest solution was to use use DB::raw with the count method.

    $count = $builder->count(DB::raw('DISTINCT b'));
    

    Remember, don't use groupBy before calling count. You can apply groupBy later, if you need it for getting rows.

    0 讨论(0)
  • 2020-12-04 16:41

    Distinct do not take arguments as it adds DISTINCT in your sql query, however, you MAY need to define the column name that you'd want to select distinct with. Thus, if you have Flight->select('project_id')->distinct()->get() is equialent to SELECT DISTINCT 'project_id' FROM flights and you may now add other modifiers like count() or even raw eloquent queries.

    0 讨论(0)
  • 2020-12-04 16:41

    Based on Laravel docs for raw queries I was able to get count for a select field to work with this code in the product model.

    public function scopeShowProductCount($query)
    {
        $query->select(DB::raw('DISTINCT pid, COUNT(*) AS count_pid'))
              ->groupBy('pid')
              ->orderBy('count_pid', 'desc');
    }
    

    This facade worked to get the same result in the controller:

    $products = DB::table('products')->select(DB::raw('DISTINCT pid, COUNT(*) AS count_pid'))->groupBy('pid')->orderBy('count_pid', 'desc')->get();
    

    The resulting dump for both queries was as follows:

    #attributes: array:2 [
      "pid" => "1271"
      "count_pid" => 19
    ],
    #attributes: array:2 [
      "pid" => "1273"
      "count_pid" => 12
    ],
    #attributes: array:2 [
      "pid" => "1275"
      "count_pid" => 7
    ]
    
    0 讨论(0)
提交回复
热议问题