Home > Article > PHP Framework > 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:
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:
##3. min methodSyntax:$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: The return value is also in the form of characters. Results in sql tracking information: Information in database: IV. avg methodSyntax:
$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: The return value is also in the form of characters. Results in sql tracking information: Information in database:
##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 :
thinkphp tutorialThe 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!