hyperf框架聚合查询(count, max, min, avg, sum)基础使用方法是单个查询的
- $count = Db::table('user')->count('id');
- $count = Db::table('user')->sum('number');
需要查询多个字段,如sql语句:
select sum(number),sum(number1),sum(number2),sum(number3) from user
hyperf框架内使用 first() 实现同时使用聚合查询多个字段
- $count = DB::table('user')->where('p_id', '=', 1)
- ->first(array(
- DB::raw('SUM(number) as number'),
- DB::raw('SUM(number1) as number1'),
- DB::raw('SUM(number2) as number2'),
- DB::raw('COUNT(id) as count'),
- ));
结果如下,返回一个数组
- $count =["number"=>"3595.0000",
- "number1"=>"12187.0500",
- "number2"=>"12187.0500",
- "count"=>841]