使用php将mysql结果转换为数据透视表
Posted
技术标签:
【中文标题】使用php将mysql结果转换为数据透视表【英文标题】:mysql result conversion to pivot table with php 【发布时间】:2018-01-24 15:01:54 【问题描述】: products(prod_id, prod_name,...)
attribute_key(attr_key_id, attr_name)
attributes_values(attr_val_id, attr_key_id, value)
product_attr_values(id,product_id,attr_val_id)
我已将 mysql 结果定义为数组定义。问题是我必须 将同一产品的相同属性与不同值连接起来 逗号(;)。但是我可以在不连接它的情况下得到结果。帮助表示赞赏。
$shop = Array
(
"0" => Array
(
"id" => 9,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Author',
"value" => 'sabsyasachi das'
),
"1" => Array
(
"id" => 10,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Author',
"value" => 'Ela Datta'
),
"2" => Array
(
"id" => 11,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Editor',
"value" => 'Sathi Basu'
),
"3" => Array
(
"id" => 12,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Editor',
"value" => 'Radha Prasad Gupta'
),
"4" => Array
(
"id" => 13,
"prodname" => 'Ramkinkar Baij',
"attribute_name" => 'Place_of_Publication',
"value" => 'Panagarh, Burdwan, West Bengal'
),
"5" => Array
(
"id" => 14,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Author',
"value" => 'sabsyasachi das'
),
"6" => Array
(
"id" => 15,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Author',
"value" => 'Ela Datta'
),
"7" => Array
(
"id" => 16,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Translated_by',
"value" => 'partha dasgupta'
),
"8" => Array
(
"id" => 17,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Editor',
"value" => 'Bandana Mukhopadhay'
),
"9" => Array
(
"id" => 18,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Editor',
"value" => 'Sathi Basu'
),
"10" => Array
(
"id" => 19,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Translated_Title',
"value" => '(Second World Telugu Conference, 1981)'
),
"11" => Array
(
"id" => 20,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Place_of_Publication',
"value" => 'Hyderabad'
),
"12" => Array
(
"id" => 21,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Foreword',
"value" => 'Information on Government organisations of Art and Culture in Andhra Pradesh'
),
"13" => Array
(
"id" => 22,
"prodname" => 'Rendava Prapancha Telugu Mahasabhalu, 1981',
"attribute_name" => 'Descriptive_Tags',
"value" => 'Conference, Language'
)
);
$groups = array();
foreach($shop as $key => $array)
//$type is not necessary, it's just for clarity below
$product = $array['prodname'];
if( !isset($groups[$product]) )
$groups[$product] = array();
$groups[$product]['prod'] = $array['prodname'];
$groups[$product][$array["attribute_name"]] = $array['value'];
//then combine the groups into a master array
$out = array();
foreach($groups as $g)
$out[] = $g;
echo '<pre>'. print_r($out, true).'</pre>';
print "<pre>";
//print_r($groups);
print "</pre>";
【问题讨论】:
这是一个分号。但是为什么你必须连接任何东西呢? 不。陈述 B 不是陈述 A 的必然推论。 同样。见:Why should I provide an MCVE for what seems to me to be a very simple SQL query? @Strawberry,请找到这个SQL fiddle 感谢进度报告。 【参考方案1】:使用 MySQL 的 GROUP BY
和 GROUP_CONCAT
函数。
SELECT prod_name, GROUP_CONCAT(CONCAT(attr_name, ': ', value) SEPARATOR ', ') AS attributes FROM [your tables and joins] GROUP BY prod_id
【讨论】:
@tantau 你是对的,但是根据我的需要从 mysql 获取结果非常困难。所以我尝试用php转换。我需要这样的东西array("0"=> array('productName'=> 'x', 'author' => 'del Karnegie,William fulckner', 'editor'=> 'x,y,z', ))
或者可能是这样的array("0"=>array('ProductName'=> 'X', 'author1'=> 'x', 'author2'=>'y', 'author3'=>'z'))
@ShuvadeepChakraborty 您还可以将属性作为 JSON 返回,以便在 php 中更好地处理它们。见***.com/questions/12511933/…。较新的 MySQL 版本甚至具有创建 JSON 的本机函数。
请帮我一个忙。你能回顾一下这个查询的效果吗[SQLfiddle] (sqlfiddle.com/#!9/cc65ed/4)以上是关于使用php将mysql结果转换为数据透视表的主要内容,如果未能解决你的问题,请参考以下文章