您的位置 首页 php

thinkphp中统计查询的方法介绍

在ThinkPHP中系统提供以下几个查询方法的使用,方便于在后期需要做统计的使用:

count() 表示查询表中总的记录数

max() 表示查询某个字段的最大值

min() 表示查询某个字段的最小值

avg() 表示查询某个字段的平均值

sum() 表示求出某个字段的总和

一、count方法

语法:

$model -> [where() -> ] count();

案例:查询部门表中的总记录数。

    //count方法    public function test(){        //实例化模型        $model = M('Dept');        //count方法        $result = $model -> count();        //打印        dump($result);     }

显示结果:

1.jpg

返回值是字符的形式。

sql跟踪信息中的结果:

2.jpg

数据库中的信息:

3.jpg

二、max方法

语法:

$model -> max('字段名');

案例:查询部门表中id最大的部门。

在以后开发的时候会有一个应用是通过max方法查询最后注册会员的id。

    //max方法    public function test(){        //实例化模型        $model = M('Dept');        //max方法        $result = $model -> max('id');        //打印        dump($result);    }

显示结果:

4.jpg

返回值是字符的形式。

sql跟踪信息中的结果:

5.jpg

数据库中的信息:

6.JPG

三、min方法

语法:

$model -> min('字段名')

案例:查询部门表中id最小的部门。

在以后开发的时候会有一个应用是通过min方法查询最早注册会员的id。

    //min方法    public function test(){        //实例化模型        $model = M('Dept');        //max方法        $result = $model -> min('id');        //打印        dump($result);    }

显示结果:

7.jpg

返回值也是字符的形式。

sql跟踪信息中的结果:

8.jpg

数据库中的信息:

9.jpg

四、avg方法

语法:

$model -> avg('字段名');

案例:求出部门表中id的平均值。

    //avg方法    public function test(){        //实例化模型        $model = M('Dept');        //max方法        $result = $model -> avg('id');        //打印        dump($result);    }

显示结果:

1.jpg

返回值也是字符的形式。

sql跟踪信息中的结果:

2.jpg

数据库中的信息:

3.jpg

五、sum方法

语法:

$model -> sum('字段名');

案例:查询字段id的总和。

    //sum方法    public function test(){        //实例化模型        $model = M('Dept');        //max方法        $result = $model -> sum('id');        //打印        dump($result);    }

显示结果:

4.jpg

返回值同样是字符的形式。

sql跟踪信息中的结果:

5.jpg

数据库中的信息:

6.JPG

推荐教程:thinkphp教程

文章来源:智云一二三科技

文章标题:thinkphp中统计查询的方法介绍

文章地址:https://www.zhihuclub.com/27873.shtml

关于作者: 智云科技

热门文章

网站地图