php 模拟mysql group by分组
程序员文章站
2022-04-05 20:20:41
...
无详细内容 无 ?php$arrs = array( array('id'=3,'year'=2014,'month'=6,'day'=2,'name'='test1','money'=10233,'rebate'=111), array('id'=7,'year'=2017,'month'=6,'day'=2,'name'='test2','money'=12.36,'rebate'=100), array('id'=5,'year'=2014,'month'=
3,'year'=>2014,'month'=>6,'day'=>2,'name'=>'test1','money'=>10233,'rebate'=>111), array('id'=>7,'year'=>2017,'month'=>6,'day'=>2,'name'=>'test2','money'=>12.36,'rebate'=>100), array('id'=>5,'year'=>2014,'month'=>6,'day'=>2,'name'=>'test1','money'=>10233,'rebate'=>111), array('id'=>11,'year'=>2017,'month'=>6,'day'=>2,'name'=>'test2','money'=>12.36,'rebate'=>100), ); /*group by 后面的字段 可任意增加或减少*/ $groupBy= array('year','month','day'); /*order by 后面的字段,可任意增加或减少*/ $orderBy = array('money'=>'asc','id'=>'desc'); /*操作 格式: mysql内置函数=>字段名|结果别名 */ $option = array('sum'=>'money|sum_money','average'=>'money|average_money,rebate','count'=>'id|total','group_concat'=>'distinct:name|concat_name,rebate'); //Func::p(Func::handdleData($arrs, $groupBy, $orderBy, $option)); Func::p(Func::resultOrderBy($arrs,$orderBy)); class Func{ static function p($data){ echo ''; print_r($data); } static function handdleData($arrs, $groupBy, $orderBy, $option){ $data = array(); foreach($arrs as $index=>$arr){ $groupKey = ''; foreach($groupBy as $v){ $groupKey .= $arr[$v].'-'; } $groupKey = trim($groupKey,'-'); if(isset($data[$groupKey])) array_push($data[$groupKey],$index); else $data[$groupKey][]=$index; } $data = Func::resultGroupBy($arrs,$data,$groupBy,$option); $data = Func::resultOrderBy($data,$orderBy); return $data; } static function resultGroupBy($arrs,$temp,$groupBy,$option){ $result = array(); foreach($temp as $key=>$value){ foreach($option as $k=>$f){ $parts = explode(',',$f); $distinct = array(); foreach($parts as $part){ $exarr = explode('|',$part); $filed = $exarr[0]; $aliasKey = isset($exarr[1]) ? $exarr[1] : ''; $aliasKey = !empty($aliasKey)?$aliasKey:$k.'_'.$filed; if($k=='sum'){ $aliasValue = 0; foreach($value as $v){ $aliasValue += $arrs[$v][$filed]; } }elseif($k=='average'){ $aliasValue = 0; foreach($value as $v){ $aliasValue += $arrs[$v][$filed]; } $aliasValue = (float)$aliasValue/count($temp[$key]); }elseif($k=='count'){ $aliasValue = count($value); }elseif($k=='group_concat'){ if(strpos($filed,':')){ $distinct = explode(':', $filed); $filed = $distinct[1]; } $aliasValue = array(); foreach($value as $v){ $aliasValue[] = $arrs[$v][$filed]; } $aliasValue = !empty($distinct) ? implode(',',array_unique($aliasValue)) : implode(',',$aliasValue); } $result[$key][$aliasKey] = $aliasValue; } } } foreach ($result as $key => &$value) { $key = explode('-', $key); for ($i = 0; $i $value) { $temp = array(); for($i = 0; $i
推荐阅读
-
MySQL group by对单字分组序和多字段分组的方法讲解
-
MySQL数据库Group by分组之后再统计数目Count(*)与不分组直接统计数目的区别
-
解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
-
MySQL分组查询group by实例
-
解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'userinfo.
-
MySQL数据库:group分组
-
mysql group by 对多个字段进行分组操作
-
MySQL --- group by 分组 取最大值,百万数据查询0.8s- 1s
-
【mysql】sql语句分组查询遇到错误:this is incompatible with sql_mode=only_full_group_by,兼容性解决
-
MySQL数据库分组查询group by语句详解