search
HomeDatabaseMysql TutorialWhat is the usage of mysql aggregate function?

What is the usage of mysql aggregate function?

Oct 19, 2020 pm 04:23 PM
mysqlaggregate 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

The execution statement will return the following results:

  Total 
  ----------- 
  1837

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 所在地 = “北美洲”

The return result is as follows:

平均金额 
--------------------- 
862.3075

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

The return result is as follows:

大订单数量 
---------------------- 
3

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

The returned result is:

所在地数量 
-------------------- 
7

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

The returned result is:

所在地数量
-------------------- 
3

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

The return result is as follows:

  最大的订单 
  --------------------- 
  2517.58

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 所在地

The returned result is as follows:

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

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
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
What are some tools you can use to monitor MySQL performance?What are some tools you can use to monitor MySQL performance?Apr 23, 2025 am 12:21 AM

How to effectively monitor MySQL performance? Use tools such as mysqladmin, SHOWGLOBALSTATUS, PerconaMonitoring and Management (PMM), and MySQL EnterpriseMonitor. 1. Use mysqladmin to view the number of connections. 2. Use SHOWGLOBALSTATUS to view the query number. 3.PMM provides detailed performance data and graphical interface. 4.MySQLEnterpriseMonitor provides rich monitoring functions and alarm mechanisms.

How does MySQL differ from SQL Server?How does MySQL differ from SQL Server?Apr 23, 2025 am 12:20 AM

The difference between MySQL and SQLServer is: 1) MySQL is open source and suitable for web and embedded systems, 2) SQLServer is a commercial product of Microsoft and is suitable for enterprise-level applications. There are significant differences between the two in storage engine, performance optimization and application scenarios. When choosing, you need to consider project size and future scalability.

In what scenarios might you choose SQL Server over MySQL?In what scenarios might you choose SQL Server over MySQL?Apr 23, 2025 am 12:20 AM

In enterprise-level application scenarios that require high availability, advanced security and good integration, SQLServer should be chosen instead of MySQL. 1) SQLServer provides enterprise-level features such as high availability and advanced security. 2) It is closely integrated with Microsoft ecosystems such as VisualStudio and PowerBI. 3) SQLServer performs excellent in performance optimization and supports memory-optimized tables and column storage indexes.

How does MySQL handle character sets and collations?How does MySQL handle character sets and collations?Apr 23, 2025 am 12:19 AM

MySQLmanagescharactersetsandcollationsbyusingUTF-8asthedefault,allowingconfigurationatdatabase,table,andcolumnlevels,andrequiringcarefulalignmenttoavoidmismatches.1)Setdefaultcharactersetandcollationforadatabase.2)Configurecharactersetandcollationfor

What are triggers in MySQL?What are triggers in MySQL?Apr 23, 2025 am 12:11 AM

A MySQL trigger is an automatically executed stored procedure associated with a table that is used to perform a series of operations when a specific data operation is performed. 1) Trigger definition and function: used for data verification, logging, etc. 2) Working principle: It is divided into BEFORE and AFTER, and supports row-level triggering. 3) Example of use: Can be used to record salary changes or update inventory. 4) Debugging skills: Use SHOWTRIGGERS and SHOWCREATETRIGGER commands. 5) Performance optimization: Avoid complex operations, use indexes, and manage transactions.

How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version