Laravel Select Count where Query

时间:2019-04-15 15:44:19

标签: mysql laravel eloquent

我的数据库中有“订单”表和“状态”列,状态存储为[1,2,3,4],这对应于[已发布,待处理,已交付,已交付]的前端,现在我需要获取每个状态在2个日期之间的计数,

请注意:我需要每个状态的计数,

发行:80 待定:50 途中:20 已交付:170

我尝试下面的代码,但没有解决我的需求

$account = DB::table('order')
                    ->whereBetween('created_at',[$fromdate, $todate])
                    ->select(DB::raw('COUNT(order.status) as total'))
                    ->get();
                    return response()->json($account,200);

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:2)

$account = DB::table('order')
    ->select(DB::raw('COUNT(order.id) as total'),'status')
    ->whereBetween('created_at',[$fromdate, $todate])
    ->whereIn('status',[1,2,3,4])
    ->groupBy('status')
    ->get();

return response()->json($account,200);

答案 1 :(得分:0)

DB::table('order')
    ->select(DB::raw('COUNT(*) as total')
    ->whereBetween('created_at',[$fromdate, $todate])
    ->groupBy('status')
    ->get();