MySQL’s AVG() function is used to calculate the average of numeric values. It supports a variety of usages, including: Calculate the average quantity of all sold products: SELECT AVG(quantity_sold) FROM sales; Calculate the average price: AVG(price); Calculate the average sales volume: AVG(quantity_sold * price). The AVG() function ignores NULL values, use IFNULL() to calculate the average of non-null values.
Usage of AVG() function in MySQL
The AVG() function is used to calculate a set of numeric values average value. It is an aggregate function, which means it aggregates values from a set of rows into a single result.
Syntax:
<code class="sql">AVG(expression)</code>
Where:
Usage example:
Suppose we have a table named "sales" with the following columns:
#To calculate the average quantity of all products sold, we can use the following query:
<code class="sql">SELECT AVG(quantity_sold) FROM sales;</code>
The result will be a single row containing the average quantity value.
Other uses:
The AVG() function can also be used to calculate other types of averages, such as:
AVG(price)
AVG(quantity_sold * price)
Notes:
The above is the detailed content of How to use avg in mysql. For more information, please follow other related articles on the PHP Chinese website!