groupby 分组后 count 计算总数的问题
1. 运行环境
1). 当前使用的 Laravel 版本?
“illuminate/database”: “v9.52.15”,
2). 当前使用的 php/php-fpm 版本?
PHP 版本:8.0.2
2. 问题描述?
groupby分组后链接调用count计算总条数,只返回了分组后第一条记录数作为总数
生成的sql语句有问题
3. 您期望得到的结果?
4. 您实际得到的结果?
最终返回结果是 1
目前解法:
这种写法有问题,会多走一次全部的查询
$count = StatisticsTargetEveryday::where($where)->groupBy($group)->get()->count();
这种写法感觉不太优雅,distinct还需要传入固定参数
$count = StatisticsTargetEveryday::where($where)->distinct(‘target’)->count();
求更好的实现方法