search
HomePHP FrameworkThinkPHPIntroduction to statistical query methods in thinkphp

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.