21,893
社区成员
发帖
与我相关
我的任务
分享
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]=上海,苏州
),
);
基本查询
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)
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
指令串拆行看的清楚些,注意有大写的那些地方
$sql="select a.title,c.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";
<?php
$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);
function array_group_concat($ar, $field, $group_by) {
$res = array();
foreach($ar as $row) {
$g = $row[$group_by];
if(isset($res[$g])) $res[$g][$field] .= ',' . $row[$field];
else $res[$g] = $row;
}
return array_values($res);
}
例:
按 title 分组取 name$arr = array(
array('title'=>'111','name'=>'上海'),
array('title'=>'111','name'=>'杭州'),
array('title'=>'dd','name'=>'上海'),
array('title'=>'dd','name'=>'宁波'),
array('title'=>'dd','name'=>'无锡'),
);
print_r(array_group_concat($arr, 'name', 'title'));
Array
(
[0] => Array
(
[title] => 111
[name] => 上海,杭州
)
[1] => Array
(
[title] => dd
[name] => 上海,宁波,无锡
)
)
按 name 分组取 title$arr = array(
array('title'=>'111','name'=>'上海'),
array('title'=>'111','name'=>'杭州'),
array('title'=>'dd','name'=>'上海'),
array('title'=>'dd','name'=>'宁波'),
array('title'=>'dd','name'=>'无锡'),
);
print_r(array_group_concat($arr, 'title', 'name'));
Array
(
[0] => Array
(
[title] => 111,dd
[name] => 上海
)
[1] => Array
(
[title] => 111
[name] => 杭州
)
[2] => Array
(
[title] => dd
[name] => 宁波
)
[3] => Array
(
[title] => dd
[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);