Home  >  Article  >  PHP Framework  >  How to use sum of queries and examples in ThinkPHP5

How to use sum of queries and examples in ThinkPHP5

PHPz
PHPzOriginal
2023-04-07 09:28:05755browse

ThinkPHP5 is a widely used high-performance PHP development framework that provides many convenient functions and methods to help developers quickly build powerful Web applications. Among them, the query function is an important function often used in ThinkPHP5 development. This article will introduce the usage and examples of query sum in ThinkPHP5.

1. How to use the query sum

In ThinkPHP5, the query sum can be achieved by using the query and sum methods. Among them, the query method is used to construct a SQL statement, and the sum method is used to execute the SQL statement and retrieve the sum in the query result.

Specifically, the query sum is used as follows:

//导入命名空间
use think\Db;

//构建SQL语句
$result = Db::query('SELECT SUM(column_name) AS total FROM table_name');

//取出查询结果中的和
$sum = $result[0]['total'];

In the above code, we first import the think\Db namespace, and then use the query method to build the SQL statement. Among them, SUM(column_name) is used to calculate the sum of all values ​​in column column_name, and AS total is used to set the name of the calculation result to total. Next, we use (execute) the query method to execute the SQL statement and take out the sum in the query result, which is $result0.

2. Example of query sum

The following uses an example to introduce the use of query sum in ThinkPHP5.

Suppose there is a table named students in our database, which contains the performance information of each student, as shown below:

id  name  grade1  grade2  grade3
1   张三   80      90      85
2   李四   75      82      93
3   王五   90      85      87
...

We need to query the total scores of all students and follow Sort by total score in descending order. We can use the following code to achieve this:

//导入命名空间
use think\Db;

//构建SQL语句
$sql = "SELECT id, name, (grade1 + grade2 + grade3) AS total_grade FROM students ORDER BY total_grade DESC";

//执行SQL语句并取出查询结果中的和
$results = Db::query($sql);

//输出查询结果
echo "<table>";
echo "<tr><th>ID</th><th>姓名</th><th>总成绩</th></tr>";
foreach($results as $result){
    echo "<tr><td>".$result['id']."</td><td>".$result['name']."</td><td>".$result['total_grade']."</td></tr>";
}
echo "</table>";

In the above code, we first import the think\Db namespace and then use a SELECT statement to build the SQL statement. Among them, (grade1 grade2 grade3) is used to calculate the total grade of each student, and the name of the calculation result is set to total_grade. Next, we use the (execution) query method to execute the SQL statement and retrieve the query results. Finally, we output the query results on the page.

3. Summary

This article introduces the usage and examples of query sum in ThinkPHP5. By studying this article, you can learn that you can quickly query the sum in the database using the query and sum methods, and use this to implement complex business logic, which will bring convenience to your development work.

The above is the detailed content of How to use sum of queries and examples in ThinkPHP5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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