Laravel独特计数 [英] Laravel Distinct Count

查看:94
本文介绍了Laravel独特计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用laravel进行此查询? DB :: raw或雄辩的用法无关紧要.

Any way to make this query work using laravel? DB::raw or Eloquent usage doesn't matter.

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"}])

我只想计数15.

推荐答案

您可以通过以下方式简单地将get替换为count:

you can simply replace get with count in this way:

$count = DB::table('tablename')->count(DB::raw('DISTINCT name'));

还可以:

DB::table('tablename')->distinct('name')->count('name');

这篇关于Laravel独特计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆