Home >PHP Framework >ThinkPHP >Introduction to statistical query methods in thinkphp

Introduction to statistical query methods in thinkphp

尚
forward
2020-04-18 09:09:445308browse

Introduction to statistical query methods in thinkphp

In ThinkPHP, the system provides the following query methods to facilitate the use of statistics in the later stage:

count() represents the total number of queries in the query table Number of records

max() means querying the maximum value of a certain field

min() means querying the minimum value of a certain field

avg( ) means querying the average value of a certain field

sum() means finding the sum of a certain field

1. count method

Syntax:

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

Case: Query the total number of records in the department table.

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

Display results:

Introduction to statistical query methods in thinkphp

The return value is in the form of characters.

Results in sql tracking information:

Introduction to statistical query methods in thinkphp

Information in database:

Introduction to statistical query methods in thinkphp

2. max method

Syntax:

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

Case: Query the department with the largest id in the department table.

In future development, there will be an application that queries the id of the last registered member through the max method.

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

Display results:

Introduction to statistical query methods in thinkphp

The return value is in the form of characters.

Results in sql tracking information:

Introduction to statistical query methods in thinkphp

Information in database:

Introduction to statistical query methods in thinkphp

##3. min method

Syntax:

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

Case: Query the department with the smallest id in the department table.

In future development, there will be an application that queries the id of the earliest registered member through the min method.

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

Display results:

Introduction to statistical query methods in thinkphp

The return value is also in the form of characters.

Results in sql tracking information:

Introduction to statistical query methods in thinkphp

Information in database:

Introduction to statistical query methods in thinkphp

IV. avg method

Syntax:

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

Case: Find the average value of ids in the department table.

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

Display results:

Introduction to statistical query methods in thinkphp

The return value is also in the form of characters.

Results in sql tracking information:

Introduction to statistical query methods in thinkphp

Information in database:

Introduction to statistical query methods in thinkphp##5. sum method

Syntax:

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

Case: Query the sum of field ids.

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

Display results:

Introduction to statistical query methods in thinkphpThe return value is also in the form of characters.

Results in sql tracking information:

Introduction to statistical query methods in thinkphpInformation in the database:

Introduction to statistical query methods in thinkphpRecommended tutorial :

thinkphp tutorial

The above is the detailed content of Introduction to statistical query methods in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete