Home Database Mysql Tutorial What is the usage of mysql aggregate function?

What is the usage of mysql aggregate function?

Oct 19, 2020 pm 04:23 PM
mysql aggregate function

Mysql aggregate function usage: 1. Use the SELECT statement to return the total number of series values, the code is [SELECT SUM (quantity) AS total number]; 2. Use the AVG function to calculate the average, the code is [SELECT AVG (unit price) *Quantity) As Average Amount].

What is the usage of mysql aggregate function?

##More related free learning recommendations: mysql tutorial(Video)

mysql aggregate function usage:

1. SUM function:

Let's start with the SUM function. This function is usually used in a SELECT statement to return the total number of series values. Assume that the product project manager wants to know the total sales of the product so far, then we can use the following query script:

  SELECT SUM(数量) AS 总数 
  FROM ProductOrders
Copy after login

The execution statement will return the following results:

  Total 
  ----------- 
  1837
Copy after login

2, AVG The function (average function)

is used similarly to SUM, it gives us the arithmetic mean of the series values. This time we can try a slightly more complex task: find the average value of all orders in the North American continent. Note that we need to multiply the "Quantity" column and the "Unit Price" column to calculate the total amount of each order. The query script is as follows:

SELECT AVG(单价* 数量) As 平均金额 
FROM ProductOrders 
WHERE 所在地 = “北美洲”
Copy after login

The return result is as follows:

平均金额 
--------------------- 
862.3075
Copy after login

3. COUNT counting function

SQL provides the COUNT function to query if the set criteria are met The number of records. We can use the COUNT(*) syntax alone to retrieve the number of rows in a table. In addition, you can also use the WHERE clause to set counting conditions and return the number of specific records. For example, let's say our product sales manager wants to know how many orders for more than 100 products the company has processed. The following is a SQL query script that satisfies this condition:

SELECT COUNT(*) AS '大订单数量'
FROM ProductOrders
WHERE 数量> 100
Copy after login

The return result is as follows:

大订单数量 
---------------------- 
3
Copy after login

The COUNT function also allows the use of the DISTINCT keyword and expression to calculate the value that satisfies the expression in the target data The number of occurrences. Likewise, you can use the ALL keyword to return the total number of values ​​that satisfy an expression, regardless of whether there are duplicate values. For example, a product manager wants to return the number of "locations" in the database through a simple query.

First, let’s take a look at the query using the ALL keyword:

SELECT COUNT(ALL 所在地) As '所在地数量'
FROM ProductOrders
Copy after login

The returned result is:

所在地数量 
-------------------- 
7
Copy after login

Obviously this is not the result we need. Because according to the ProductOrders table, there are only three locations for all orders, namely North America, Africa, and Europe. Let's use the DISTINCT keyword instead:

SELECT COUNT(DISTINCT 所在地) As '所在地数量'
FROM ProductOrders
Copy after login

The returned result is:

所在地数量
-------------------- 
3
Copy after login

This is the result we want.

4. Maximum and minimum values

In the last section of this article, let’s take a look at what SQL provides us with to find the maximum value that satisfies a given expression. function of value and minimum value. The MAX() function returns the maximum value in a given data set. We can give the function a field name to return the maximum value of a given field in the table. You can also use expressions and GROUP BY clauses in the MAX() function to enhance the search function.

Still in the ProductOrders table, suppose our product manager wants to find the order that brings the most revenue to the company from this database. We can use the following query to find this order and return the total sales amount of the order:

  SELECT MAX(数量 * 单价)As '最大的订单' 
  FROM ProductOrders
Copy after login

The return result is as follows:

  最大的订单 
  --------------------- 
  2517.58
Copy after login

The MIN() function is used similarly, but returns an expression the minimum value. Let's try a slightly more complex query using the MIN() function. Our sales department is currently analyzing data for small orders. They want to find out the minimum order for each location. In addition to calculating the value in the expression, this also requires the use of a GROUP BY clause to summarize the data at the location. The SQL query is as follows:

SELECT 所在地, MIN(数量 * 单价) AS '最小订单'
FROM ProductOrders 
GROUP BY 所在地
Copy after login

The returned result is as follows:

所在地       最小订单
------------- --------------------- 
非洲         167.04
欧洲        2099.02
北美洲    70.65
Copy after login

The above is the detailed content of What is the usage of mysql aggregate function?. For more information, please follow other related articles on the PHP Chinese website!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to recover data after SQL deletes rows How to recover data after SQL deletes rows Apr 09, 2025 pm 12:21 PM

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

How to check SQL statements How to check SQL statements Apr 09, 2025 pm 04:36 PM

The methods to check SQL statements are: Syntax checking: Use the SQL editor or IDE. Logical check: Verify table name, column name, condition, and data type. Performance Check: Use EXPLAIN or ANALYZE to check indexes and optimize queries. Other checks: Check variables, permissions, and test queries.

See all articles