php Laravel 不同计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35868551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Distinct Count
提问by saimcan
Any way to make this query work using laravel? DB::raw or Eloquent usage doesn't matter.
有什么办法可以让这个查询使用 laravel 工作吗?DB::raw 或 Eloquent 的用法无关紧要。
SELECT count(DISTINCT name) FROM tablename;
Here's what i've tried but cannot get the proper output:
这是我尝试过但无法获得正确输出的内容:
EloquentTableName::select(DB::raw('count(DISTINCT name) as name_count'))->get();
This returns something like this and i'd like to fix that:
这会返回这样的东西,我想解决这个问题:
([{"name_count":"15"}])
I just want to get count 15.
我只想数到 15。
回答by Gouda Elalfy
you can simply replace get with count in this way:
您可以通过这种方式简单地将 get 替换为 count :
$count = DB::table('tablename')->count(DB::raw('DISTINCT name'));
also can do:
还可以:
DB::table('tablename')->distinct('name')->count('name');
回答by Alisha Lamichhane
You may simply do the following:
您可以简单地执行以下操作:
Tablename::distinct()->count('name');
回答by Chandan Mistry
DB::table('tablename')->distinct()->count('name');
is the correct answer.
是正确答案。
->distinct('name')does notwork in Laravel.
- >不同的(“名称”)不工作Laravel。
回答by Laerte
Try this:
尝试这个:
$result = TableName::groupBy('name')->count();
Hope it helps...
希望能帮助到你...