Skip to content Skip to sidebar Skip to footer

Laravel Join With Only Limiting 2 Row

so i having this kind of query where i do grouping with max date/period like this $table_data = App\salesreport::join(DB::RAW('(SELECT company_id, MAX(period) AS max_period FROM sa

Solution 1:

Using this trick:

App\salesreport::join(DB::RAW('(SELECT company_id, GROUP_CONCAT(periods ORDER BY periods DESC) grouped_periods FROM salesreport GROUP BY company_id ) latest_report'),function($join){
        $join->on('salesreport.company_id','=','latest_report.company_id');
        $join->whereBetween(DB::raw('FIND_IN_SET(`salesreport`.`periods`, `latest_report`.`grouped_periods`)'), [1, 2]);
    })->get();

Solution 2:

Using this trick:

App\salesreport::join(DB::RAW('(SELECT company_id, GROUP_CONCAT(periods ORDER BY periods DESC) grouped_periods FROM salesreport GROUP BY company_id ) latest_report'),function($join){
        $join->on('salesreport.company_id','=','latest_report.company_id');
        $join->whereBetween(DB::raw('FIND_IN_SET(`salesreport`.`periods`, `latest_report`.`grouped_periods`)'), [1, 2]);
    })->get();

Post a Comment for "Laravel Join With Only Limiting 2 Row"