dqndc26628 2018-03-23 14:30
浏览 76
已采纳

laravel加入仅限制2行

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 salesreport GROUP BY company_id ) latest_report'),function($join){
            $join->on('salesreport.company_id','=','latest_report.company_id');
            $join->on('salesreport.periods','=','latest_report.max_periods');
    })->get()

and it work just fine, it can group all company into one and then showing only the latest one MAX(period). And then i want to tweak it more, how about if i want to show not only the latest one, but also the latest two of each company? so each company will returning 2 report that is the latest of all report.

and then i think maybe adding LIMIT 2 will make it works so i put it like this

$table_data = App\salesreport::join(DB::RAW('(SELECT company_id, MAX(period) AS max_period FROM salesreport GROUP BY company_id ) latest_report'),function($join){
            $join->on('salesreport.company_id','=','latest_report.company_id');
            $join->on('salesreport.periods','<=','latest_report.max_periods');
            $join->limit(2)
    })->get()

but it has no effect and just showing all report of all company which have period <= max_periods.

to make things more clearer here is my table that i do joining

so here is my sales report table

+----+------------+-------+------------+
| id | company_id | price |  periods   |
+----+------------+-------+------------+
|  1 | A1         |   500 | 2016-07-12 |
|  2 | A2         |   540 | 2017-01-21 |
|  3 | A1         |   440 | 2017-01-19 |
|  4 | A1         |   440 | 2018-01-19 |
|  5 | A2         |   330 | 2016-01-12 |
|  6 | A2         |   333 | 2018-01-22 |
+----+------------+-------+------------+

and then using the first query up there then i will get this kind of table

+----+------------+-------+------------+
| id | company_id | price |  periods   |
+----+------------+-------+------------+
|  4 | A1         |   440 | 2018-01-19 |
|  6 | A2         |   333 | 2018-01-22 |
+----+------------+-------+------------+

so what i want is to get this kind of table

+----+------------+-------+------------+
| id | company_id | price |  periods   |
+----+------------+-------+------------+
|  4 | A1         |   440 | 2018-01-19 |
|  3 | A1         |   440 | 2017-01-19 |
|  6 | A2         |   333 | 2018-01-22 |
|  2 | A2         |   540 | 2017-01-21 |
+----+------------+-------+------------+

so the latest 2 of all for each company.. is it possible? with only one request?

  • 写回答

2条回答 默认 最新

  • dongrong1856 2018-03-23 16:07
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号