我如何多次返回同一行?我尝试使用wherein('table' , \Session::get('memory'))
,但它只返回非重复项。
据我所知,联盟可以实现,但我很困惑如何实现它。
有人可以帮帮我吗?
会话数组:
array:5 [▼
0 => "addon-0a65729-aak"
1 => "visiontek-900379"
2 => "addon-0a65729-aak"
3 => "addon-0a65729-aak"
4 => "addon-0a65729-aak"
]
查询我到目前为止
$memory = \DB::table('memory')->leftjoin('price', 'price.part_number' , '=', 'memory.part_number')->groupby('memory.part_number')
->select(
'memory.part_number as part_number',
\DB::raw('min(price.price) as price'),
'price.url as url',
'memory.slug as slug');
foreach (\Session::get('memory') as $key) {
$memory = $memory->union($memory)->where('memory.slug', $key);
}
$memory = $memory->get();
查询将返回的正确输出:
------------------------------------------------
| part-Number| Price | URL | SLUG |
|----------------------------------------------|
|111111 | 123.3 |http://| addon-0a65729-aak|
|133111 | 145.3 |http://| visiontek-900379 |
|111111 | 123.3 |http://| addon-0a65729-aak|
|111111 | 123.3 |http://| addon-0a65729-aak|
|111111 | 123.3 |http://| addon-0a65729-aak|
------------------------------------------------
修改
记忆表:
memory table
==================================================
id | part_number | Slug
==================================================
1 |CMZ8GX3M2A1600C9 | corsair-cmz8gx3m2a1600c9
2 |F4-3200C16D-16GVK | gskill-f4-3200c16d-16gvk
3 |F3-12800CL9D-8GBRL| gskill-f3-12800cl9d-8gbrl
4 |CMK32GX4M2A2666C16| gskill-f3-1600c9d-8gao
5 |F3-12800CL9D-8GBXL| gskill-f3-12800cl9d-8gbxl
6 |F4-2400C15D-16GVR | gskill-f4-2400c15d-16gvr
7 |F4-2666C15Q-32GRKB| gskill-f4-2666c15q-32grkb
8 |F3-1600C11D-8GNT | gskill-f3-1600c11d-8gnt
9 |CMX4GX3M2A1333C8 | corsair-cmx4gx3m2a1333c8
10 |F4-3000C14Q-32GVK | gskill-f4-3000c14q-32gvk
价格表
Price table
==================================================
id | part_number | price | url
==================================================
1 |CMZ8GX3M2A1600C9 | 122.33 | http:store_url
2 |CMZ8GX3M2A1600C9 | 222.33 | http:store_url
3 |CMZ8GX3M2A1600C9 | 551.00 | http:store_url
4 |CMK32GX4M2A2666C16| 1999.99 | http:store_url
5 |F3-12800CL9D-8GBXL| 222.01 | http:store_url
6 |F4-2400C15D-16GVR | 800 | http:store_url
7 |CMZ8GX3M2A1600C9 | 19.99 | http:store_url
8 |F3-1600C11D-8GNT | 16.05 | http:store_url
9 |F4-3000C14Q-32GVK | 990.15 | http:store_url
==================================================
会话数组
Session array -
array (
CMZ8GX3M2A1600C9
CMZ8GX3M2A1600C9
CMZ8GX3M2A1600C9
CMZ8GX3M2A1600C9
F3-12800CL9D-8GBXL
CMZ8GX3M2A1600C9
F4-3000C14Q-32GVK
F3-12800CL9D-8GBXL
CMX4GX3M2A1333C8
)
输出,它应该是什么样子
Output
==========================================================================
part number | Price | url | Slug
==========================================================================
CMZ8GX3M2A1600C9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 //session val 0
CMZ8GX3M2A1600C9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 //session val 1
CMZ8GX3M2A1600C9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9 //session val 2
CMZ8GX3M2A1600C9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9
F3-12800CL9D-8GBXL| 222.01 | http:store_url | gskill-f3-12800cl9d-8gbxl
CMZ8GX3M2A1600C9 | 19.99 | http:store_url | corsair-cmz8gx3m2a1600c9
F4-3000C14Q-32GVK | 990.15 | http:store_url | gskill-f4-3000c14q-32gvk
F3-12800CL9D-8GBXL| 222.01 | http:store_url | gskill-f3-12800cl9d-8gbxl
CMX4GX3M2A1333C8 | NULL | NULL | corsair-cmx4gx3m2a1333c8 //session val n
编辑:找到解决方案 循环遍历会话数组和查询结果,并将值添加到新数组并将数组转换为对象。
$count = 0;
$newmemory = [];
foreach (\Session::get('memory') as $k => $v) {
foreach ($memory as $mem => $value) {
if ($v == $value->slug) {
foreach ($value as $key => $val) {
$newmemory[$count][$key] = $val;
}
}
}
$count++;
}
$newmemory = json_decode(json_encode($newmemory));
答案 0 :(得分:2)
现在这将是使用laravel 5.2获得预期结果的新查询。
$final="";
$cnt=0;
foreach ($array as $key => $value) {
$memory = DB::table("price as p")
->leftjoin("memory as m","m.part_number","=","p.part_number")
->select('m.part_number as part_number', DB::raw('min(p.price) as price'),'p.url as url', 'm.slug as slug')
->where("m.slug","like","%".$value."%");
if($cnt==0)
{
$cnt+=1;
$final=$memory;
}
else
{
$final=$final->unionall($memory);
}
}
$final=$final->get();
答案 1 :(得分:-1)
在您的查询中,如果您想要多个价格,则需要GroupBy('Price')
如果你想要最低的一个,你可以使用集合方法过滤或将Min()语句放在DB :: raw()
中