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

Introduction to statistical query methods in thinkphp

Release: 2020-04-18 09:09:44
forward
5315 people have browsed it

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();
Copy after login

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

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

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('字段名');
Copy after login

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);
    }
Copy after login

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('字段名')
Copy after login

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);
    }
Copy after login

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('字段名');
Copy after login

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);
    }
Copy after login

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('字段名');
Copy after login

Case: Query the sum of field ids.

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

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template