问答 / 1 / 21 / 创建于 2年前
比如我需要统计users表 status 等于 0 1 2 这几种情况 分别的数量
DB::table('***') ->selectRaw("status,count('id') as count") ->groupBy('status') ->pluck('count', 'status')->toArray();
select count(if(status=0,1,null)) status_0_num,count(if(status=1,1,null)) status_1_num,count(if(status=2,1,null)) status_2_num from users
[@tiantian10000]
User::selectRaw('count(if(status=0,1,null)) status_0_num,count(if(status=1,1,null)) status_1_num,count(if(status=2,1,null))')->first();
$query = $Plan::query() ->where('user_id', '=', $user_id) ->orderByDesc('id') ->select('id', 'type', 'user_id', 'categories_id', 'total_score') ->limit(10) ->get();
select count(*),status from users group by status
group by
可以使用 mysql 的 case then
User::query() ->selectRaw(" sum(case when status = 1 then 1 end) as status_1_total, sum(case when status = 2 then 1 end) as status_2_total, ") // ...
@jack_f
$query = $Plan::query() ->where('user_id', '=', $user_id) ->orderByDesc('id') ->select('id', 'type', 'user_id', 'categories_id', 'status') ->limit(10) ->get();
我这个查询有条件 和最后的limit 的限制 我怎么再这个查询的结果 统计 不同status的数量呀 帮帮忙看看 谢谢哈
你先查询10条出来,然后把查询放到子查询里面 from 条件,具体这里使用: $this->query = DB::table(DB::raw("({$this->query->toSql()}) as cashier_order")) ->selectRaw(' xxxxx ->mergeBindings($this->query);
我要举报该,理由是:
DB::table('***') ->selectRaw("status,count('id') as count") ->groupBy('status') ->pluck('count', 'status')->toArray();
group by
可以使用 mysql 的 case then
@jack_f
我这个查询有条件 和最后的limit 的限制 我怎么再这个查询的结果 统计 不同status的数量呀 帮帮忙看看 谢谢哈
你先查询10条出来,然后把查询放到子查询里面 from 条件,具体这里使用: $this->query = DB::table(DB::raw("({$this->query->toSql()}) as cashier_order")) ->selectRaw(' xxxxx ->mergeBindings($this->query);