[英]Laravel query builder GROUP BY method alongside with SKIP and TAKE methods
I have around 50000 records and I am showing them in Datatable with server side processing.我有大约 50000 条记录,我在服务器端处理的数据表中显示它们。 In my query I am applying the groupBy()
method with skip()
and take()
method.在我的查询中,我将groupBy()
方法与skip()
和take()
方法一起应用。
I want to be able to apply the limit AFTER groupBy()
eg我希望能够在groupBy()
之后应用限制,例如
If limit is 10 it should return 10 groups not 10 records.如果限制为 10,则应返回 10 组而不是 10 条记录。
DB::table('actions')->whereBetween('timestamp',
array($dates['startDate'], $dates['endDate']))
->where('shop_name', $shopName)
->skip($start)
->take(10)
->get()
->groupBy('product_id');
With this query i am getting 10 records not 10 groups.通过这个查询,我得到了 10 条记录,而不是 10 组。
尝试在查询的最后但在get()
之前省略take(10)
并添加limit(10)
get()
。
The order of your query statements are wrong.您的查询语句的顺序是错误的。 The following should do the trick.以下应该可以解决问题。 Notice the groupBy()
is before your take()
and get()
.请注意groupBy()
在您的take()
和get()
。
In your case, you are grouping after the records have been fetched.在您的情况下,您是在获取记录后进行分组。
DB::table('actions')
->whereBetween('timestamp', array($dates['startDate'], $dates['endDate']))
->where('shop_name', $shopName)
->groupBy('product_id')
->skip($start)
->take(10)
->get()
DB::table('actions')
->whereBetween('timestamp', array($dates['startDate'], $dates['endDate']))
->where('shop_name', $shopName)
->get()
->groupBy('product_id')
->splice($start,$rowperpage);
and this order worked.这个命令奏效了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.