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:
The return value is in the form of characters.
Results in sql tracking information:
Information in database:
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:
The return value is in the form of characters.
Results in sql tracking information:
Information in database:
$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:
$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:
##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:
The return value is also in the form of characters.
Results in sql tracking information:
Information in the database:
Recommended 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.