讲师中心 微信公众号
AI工具推荐 视频效率加速

ThinkPHP5数据库实例详解

浏览21090
更新时间2022-01-20

聚合查询

也叫统计查询,通过聚合函数对表中字段值进行统计或计算;

1、聚合函数:仅返回单值的函数

如计数(count)、最大值(max)、最小值(min)、平均值(avg)、求和(sum)等,除count外,其它方法的参数必须是可计算的数值型字段;

序号 方法 说明 参数
1 count 统计数量 参数是要统计的字段名(可选)
2 max 获取最大值 参数是要统计的字段名(必须)
3 min 获取最小值 参数是要统计的字段名(必须)
4 avg 获取平均值 参数是要统计的字段名(必须)
5 sum 获取总分 参数是要统计的字段名(必须)

2、 源码分析:

这几个方法,其内部的实现机制是一样的,都用调用之前学过的value方法实现
 ,每个方法源码很简单,在此一并贴出:

1. count方法源码:

    /**
     * COUNT查询
     * @access public
     * @param string $field 字段名
     * @return integer
     */
    public function count($field = '*')
    {
        return (int) $this->value('COUNT(' . $field . ') AS tp_count', 0);
    }

2. sum方法源码:

    /**
     * SUM查询
     * @access public
     * @param string $field 字段名
     * @return float|int
     */
    public function sum($field = '*')
    {
        return $this->value('SUM(' . $field . ') AS tp_sum', 0) + 0;
    }

3. min方法源码:

    /**
     * MIN查询
     * @access public
     * @param string $field 字段名
     * @return float|int
     */
    public function min($field = '*')
    {
        return $this->value('MIN(' . $field . ') AS tp_min', 0) + 0;
    }

4. max方法源码:

    /**
     * MAX查询
     * @access public
     * @param string $field 字段名
     * @return float|int
     */
    public function max($field = '*')
    {
        return $this->value('MAX(' . $field . ') AS tp_max', 0) + 0;
    }

5. avg方法源码:

    /**
     * AVG查询
     * @access public
     * @param string $field 字段名
     * @return float|int
     */
    public function avg($field = '*')
    {
        return $this->value('AVG(' . $field . ') AS tp_avg', 0) + 0;
    }

源码分析:

  • 聚合方法( '字段名' )  等价于  value( ' 聚合函数( 字段名 ) ') ;
  • 例如: sum( 'salary' )   <===>  value( 'sum( salary ) ' )  。

3、实例演示

任务1 :统计tp5_staff表中工资大于8000的员工人数

  • Index.php 控制器代码:
<?php
namespace app\index\controller;
//导入数据库类
use think\Db;

class Index {
  public function index(){   
   $result = Db::table('tp5_staff')  //设置数据表
           -> where(['salary'=>['>',8000]])  //设置查询条件
           -> count();   //统计满足条件的记录数量

    //查看结果
    dump($result);
  }
}
  • 查结结果:
int( 6 )     //表示满足条件的有6条记录
  • count方法默认参数为'*',所以此处可以省略;
  • 其中的的count方法可以用value方法替代
 Db::table('tp5_staff')  -> where(['salary'=>['>',8000]]) -> value( 'count( '*' )' );
  • 对应的SQL语句:
SELECT COUNT(*) AS tp_count FROM `tp5_staff` WHERE `salary` > 8000 LIMIT 1;

任务2 :统计tp5_staff表中开发部平均工资(结果取2位小数)

<?php
namespace app\index\controller;
//导入数据库类
use think\Db;

class Index {
  public function index(){   
   $result = Db::table('tp5_staff')  //设置数据表
           -> where(['dept'=>['=','开发部']])  //设置查询条件
           -> avg('salary');   //查询部门平均工资

    //查看结果
    dump(round($result,2));
  }
 }
  • 查询结果:
float(6456.43)    // 开发部员工的平均工资
  • 对应的SQL
SELECT AVG(salary) AS tp_avg FROM `tp5_staff` WHERE `dept` = '开发部' LIMIT 1
  • 同理,也可以用value方法进行改写:
 Db::table('tp5_staff')  -> where('dept','开发部') -> value( 'avg( salary )' );
  • max方法,min方法,sum方法使用方式与前面二例完全相同,此处不再举例。

4、总结

单独的统计查询其实并不常用,分组查询中其实也用到了聚合函数。聚合查询比较适合用在子查询中,后面还会有实例讲解。

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn