How to write select query with subquery using laravel Eloquent Querybuilder?

后端 未结 1 918
不知归路
不知归路 2021-01-03 10:55

I had got the result from the query.

My Simple SQL is:

 SELECT
    o2.driver_id,
    total_delieveries,
    DATE_FORMAT(o1.created_at ,\'%Y-%m-%d\')          


        
相关标签:
1条回答
  • 2021-01-03 11:32

    Also show this answer and for your variant:

    $subQuery = \DB::table('orders')->selectRaw('driver_id, created_at, COUNT(driver_id) AS total_delieveries')
        ->where('is_paid', 0)
        ->where('order_status', '5')
        ->whereBetween('created_at', [$first_Day, $last_Day])
        ->groupBy(\DB::raw('DATE_FORMAT(created_at ,"%Y-%m-%d"),driver_id'));
    
    $q = \DB::table(\DB::raw('('.$subQuery->toSql().') as o1'))
        ->selectRaw('o2.driver_id,total_delieveries,DATE_FORMAT(o1.created_at ,"%Y-%m-%d") AS created_at')
        ->join('orders as o2', 'o1.driver_id', '=', 'o2.driver_id')
        ->groupBy('o1.created_at')
        ->mergeBindings($subQuery)
        ->get();
    
    0 讨论(0)
提交回复
热议问题