Previous: [MySQL Must Know and Know (8)] [Use Data Processing Function]
+++++++++++++Start line++++++++++++++++
Article Directory
1. Aggregate function
Aggregate functions run on row groups, functions that calculate and return a single value
1.1 AVG() function
AVG() finds the average value of the column by counting the number of rows in the table and calculating the sum of the values of a specific column
mysql> SELECT AVG(prod_price) AS avg_price
-> FROM products;
AVG() can also be used to determine the average value of a specific column or row
mysql> SELECT AVG(prod_price) AS avg_price
-> FROM products
-> WHERE vend_id = 1003;
Only for a single column
AVG() can only be used to determine the average value of a specific numeric column, and the column name must be given as a function parameter. In order to obtain the average value of multiple columns, multiple AVG() functions must be used.
NULL value
AVG() function ignores rows whose column value is NULL
1.2 COUNT() function
COUNT() function to count
Returns the total number of customers in the customers table
mysql> SELECT COUNT(*) AS num_cust
-> FROM customers;
Count customers with email addresses
mysql> SELECT COUNT(cust_email) AS num_cust
-> FROM customers;
NULL value
If you specify a column name, the row whose value in the specified column is empty is ignored by the COUNT() function, but if the asterisk (*) is used in the COUNT() function, it is not ignored
1.3 MAX() function
mysql> SELECT MAX(prod_price) AS max_price
-> FROM products;
Use MAX() for non-numeric data
Although MAX() is generally used to find the largest number or date value, MySQL allows it to be used to return the maximum value in any column, including the maximum value in a text column. When used for text data, if the data is sorted by the corresponding column, MAX() returns the last row
NULL value
MAX() function ignores rows whose column value is NULL
1.4 MIN() function
mysql> SELECT MIN(prod_price) AS min_price
-> FROM products;
Use MIN() for non-numeric data
MySQL allows it to be used to return the minimum value in any column, including the minimum value in a text column. When used for text data, if the data is sorted by the corresponding column, MIN() returns the top row
NULL value
The MIN() function ignores rows whose column value is NULL
1.5 SUM() function
mysql> SELECT SUM(quantity) AS items_ordered
-> FROM orderitems
-> WHERE order_num = 20005;
SUM can also be used to total calculated values
mysql> SELECT SUM(item_price*quantity) AS total_price
-> FROM orderitems
-> WHERE order_num = 20005;
Calculate on multiple columns
Using standard arithmetic operators, all aggregate Hassan farmers can be used to perform calculations on multiple columns
NULL value
SUM() function ignores rows whose column value is NULL
Two, gather different values
ALL is the default
The ALL parameter does not need to be specified because it is the default behavior. If DISTINCT is not specified, it is assumed to be ALL
Use the AVG() function to return the average price of a product provided by a specific supplier
mysql> SELECT AVG(DISTINCT prod_price) AS avg_price
-> FROM products
-> WHERE vend_id = 1003;
note
DISTINCT must use column names and cannot be used in calculations or expressions
Three, combined aggregate function
mysql> SELECT COUNT(*) AS num_items,
-> MIN(prod_price) AS price_min,
-> MAX(prod_price) AS price_max,
-> AVG(prod_price) AS price_avg
-> FROM products;
+++++++++++++End line++++++++++++++++