我有mysql结果,如下所示。我想创建基于商店的列表,如何在PHP中进行操作?
SELECT shsv.shop_id, sv.shop, shsv.shop_product_id, shsv.shop_product, sum(shsv.cash)
FROM turerp_db.shop_has_sale_view shsv
left JOIN shop_view sv ON ( sv.shop_id = shsv.shop_id )
group by shsv.shop_id, shsv.shop_product_id
ID商店P_ID产品价格
10 SHOP_1 14 PRODUCT_1 322
11 SHOP_2 3 PRODUCT_2 2000
11 SHOP_2 5 PRODUCT_3 55
SHOP_1
PRODUCT_1
SHOP_2
PRODUCT_2
PRODUCT_3
最佳答案
您可以使用PHP数组的关联性质的力量:http://www.php.net/manual/en/language.types.array.php
$shops = [];
$shops['SHOP_1'][] = 'PRODUCT_1';
$shops['SHOP_2'][] = 'PRODUCT_2';
$shops['SHOP_2'][] = 'PRODUCT_3';
var_dump($shops)
将打印:array(2) {
["SHOP_1"]=>
array(1) {
[0]=>
string(9) "PRODUCT_1"
}
["SHOP_2"]=>
array(2) {
[0]=>
string(9) "PRODUCT_2"
[1]=>
string(9) "PRODUCT_3"
}
}
您还应该参考mysql驱动程序文档。它的某些方法可能会以类似的方式返回您的数据。