讨论数量:
假设每页10个数据,商家A总商品10个,商家B总商品5个,商家C总商品2个、记为A10、B5、C2
第一页:10/3=3,先每个商家查3个,实际A3、B3、C2,剩2再A1、B1,总计是A4、B4、C2
第二页:10/3=3,先每个商家查3个,实际A3、B1、C0,剩6再A3,总计是A6、B1、C0
我觉得这样算应该挺快了,资源消耗也没多大
关于数据重组:
//每次连续数
$continuity=2;
//原始数据
$shops=[
['id'=>1,'name'=>'商家A','goods'=>['a1','a2','a3','a4']],
['id'=>2,'name'=>'商家B','goods'=>['b1','b2','b3','b4']],
['id'=>3,'name'=>'商家C','goods'=>['c1','c2']],
];
$list=[];
do{
foreach ($shops as &$shop){
for ($i=0;$i<$continuity;$i++){
if($good=array_shift($shop['goods'])){
$list[]=$good;
}else{
break;
}
}
}
}while(count(array_merge(...array_column($shops,'goods'))));
echo json_encode($list,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
/*
* [
"a1",
"a2",
"b1",
"b2",
"c1",
"c2",
"a3",
"a4",
"b3",
"b4"
]
*/
select field from shop where shop = A limit 5;
select field from shop where shop = B limit 5;
然后在PHP层面做拼接。
换一个角度,从商家表去查,然后关联产品表呢?
$page = request()->page?:0;
$shops_count = Shop::count();
$take = ceil(10/$shops_count);
$products = Shop::with(['products' => function ($query) use ($take, $page) {
$query->take($take)->skip($take * $page);
}])->get();
典型的row_number场景
select a,b,c,d,row_number over ( partition by xx order by xx desc) as rn where rn <= 2
推荐文章: