时间:2021-07-01 10:21:17 帮助过:8人阅读
Array
(
[0] => Array
(
[title] => 111
[name] => 上海
)
[1] => Array
(
[title] => 111
[name] => 江苏
)
[2] => Array
(
[title] => dd
[name] => 上海
)
[3] => Array
(
[title] => dd
[name] => 江苏
)
[4] => Array
(
[title] => dd
[name] => 浙江
)
)
array(
[0]=>array(
[title]=111
[name]=上海,苏州
),
);
$arr = array(
array('title'=>'111','name'=>'上海'),
array('title'=>'111','name'=>'杭州'),
array('title'=>'dd','name'=>'上海'),
array('title'=>'dd','name'=>'宁波'),
array('title'=>'dd','name'=>'无锡'),
);
$title_arr = array();
$data = array();
foreach($arr as $value){
if(!in_array($value['title'],$title_arr)){
array_push($title_arr,$value['title']);
$data[] = $value;
}else{
$index = array_search($value['title'],$title_arr);
$data[$index]['name'] .= ','.$value['name'];
}
}
var_dump($data);
$arr = array(
array('title'=>'111','name'=>'上海'),
array('title'=>'111','name'=>'杭州'),
array('title'=>'dd','name'=>'上海'),
array('title'=>'dd','name'=>'宁波'),
array('title'=>'dd','name'=>'无锡'),
);
$tmp = $arr[0]['title'];
$newarr = array();
foreach($arr as $value){
$key = $tmp == $value['title'] ? $tmp : $value['title'];
$newarr[$key]['title'] = $value['title'];
$newarr[$key]['name'] .= $value['name'].',';
}
var_dump($newarr);
select a.title, GROUP_CONCAT(c.name) AS name
from information_agent as a
left join information_agent_area as b on a.id=b.tid
left join sales_area as c on b.area_id=c.id
GROUP BY a.title
基本查询
mysql> select * from aa;
+------+------+
------解决思路----------------------
id
------解决思路----------------------
name
------解决思路----------------------
+------+------+
------解决思路----------------------
1
------解决思路----------------------
10
------解决思路----------------------
------解决思路----------------------
1
------解决思路----------------------
20
------解决思路----------------------
------解决思路----------------------
1
------解决思路----------------------
20
------解决思路----------------------
------解决思路----------------------
2
------解决思路----------------------
20
------解决思路----------------------
------解决思路----------------------
3
------解决思路----------------------
200
------解决思路----------------------
------解决思路----------------------
3
------解决思路----------------------
500
------解决思路----------------------
+------+------+
6 rows in set (0.00 sec)
1.以id分组,把name字段的值打印在一行,逗号分隔(默认)
mysql> select id,group_concat(name) from aa group by id;
+------+--------------------+
------解决思路----------------------
id
------解决思路----------------------
group_concat(name)
------解决思路----------------------
+------+--------------------+
------解决思路----------------------
1
------解决思路----------------------
10,20,20
------解决思路----------------------
------解决思路----------------------
2
------解决思路----------------------
20
------解决思路----------------------
------解决思路----------------------
3
------解决思路----------------------
200,500
------解决思路----------------------
+------+--------------------+
3 rows in set (0.00 sec)