$search = Search::with(['brand'=>function($query){
return $query->select('id','brand_name');
}])
->groupBy('brand_id','part_num')
->having('brand_id','>',0)
->having('part_num','!=',0)
->orderBy('num','DESC')
->get([DB::raw('count(brand_id) as num'),'id','brand_id','part_num']);
//以上代码就是分组的代码.然后筛选出需要的字段
报错信息
SQLSTATE[42000]: Syntax error or access violation: 1055 'smart360.sm_search.id' isn't in GROUP BY (SQL: select count(brand_id) as num, id
, brand_id
, part_num
from sm_search
group by brand_id
, part_num
having brand_id
> 0 and part_num
!= 0 order by num
desc)
//上面的代码就是根据brand_id和part_num分组,然后统计最多的.倒序排序;上面的sql使用命令行执行是没错的
Sql语句
select count(brand_id) as num, id
, brand_id
, part_num
from sm_search
group by brand_id
, part_num
having brand_id
> 0 and part_num
!= 0 order by num
desc