如果我有这个问题:
select
id,
@product_rank := IF(@current_category = category_id, @product_rank + 1, 1) AS product_rank,
@current_category := category_id
from products
并使用eloquent builder创建查询:
Products::from(DB::raw('
(select
id,
@product_rank := IF(@current_category = category_id, @product_rank + 1, 1) AS product_rank,
@current_category := category_id
from products) as q
'))->where('product_rank', '<=', 10)->paginate(5);
因为laravel分页希望获得所有记录的总数,所以它会执行一个额外的查询,如下所示:
select count(*) as aggregate from (
select
id,
@product_rank := IF(@current_category = category_id, @product_rank + 1, 1) AS product_rank,
@current_category := category_id
from products
) q where product_rank <= 10
所以呢 @current_category
以及 @product_rank
不会重新初始化,主查询返回错误的结果。
我想知道是否有办法重新初始化这两个变量。
暂无答案!
目前还没有任何答案,快来回答吧!