Table of Contents
1、聚集函数
(1)、AVG()函数
(2)、COUNT()函数
(3)、MAX()函数
(4)、MIN()函数
(5)、SUM()函数
2、聚集不同值
3、组合聚集函数
Home Database Mysql Tutorial MySQL数据汇总与聚集函数――MySQL系列(七)_MySQL

MySQL数据汇总与聚集函数――MySQL系列(七)_MySQL

Jun 01, 2016 pm 01:17 PM
average value maximum value

1、聚集函数

聚集函数是运行在行组上,计算和返回单个值的函数。

SQL聚集函数 函数 说明 AVG() 返回某列的平均值 COUNT() 返回某列的行数 MAX() 返回某列的最大值 MIN() 返回某列的最小值 SUM() 返回某个列之和

(1)、AVG()函数

可以返回所有列的平均值,也可以返回特定列的平均值。
SELECT AVG(prd_price) AS avg_price FROM productsWhere vend_id=1003;
Copy after login
将过滤出vend_id=1003的产品,avg是这些产品的平均值.

(2)、COUNT()函数

COUNT(*)对表中行的数目进行计数,不管表列中包含的是空(NULL)还是非空值; COUNT(column)对特定列中有值的行进行计算,忽略NULL值。

(3)、MAX()函数

MAX()返回指定列的最大值,要求指定列名,忽略NULL值。在MySQL中,MAX()函数可以对非数据列使用,在用于文本数据时,如果数据按相应的列排序,MAX()返回最后一行。

(4)、MIN()函数

MIN()返回指定列的最小值,要求指定列名,忽略NULL值。
在MySQL中,MIN()函数可以对非数据列使用,在用于文本数据时,如果数据按相应的列排序,MIN()返回最前面的一行。

(5)、SUM()函数

用来返回指定列的和(总计),忽略NULL值的行。
SELECT SUM(num) AS prod_sum---返回指定订单号中的商品数量FROM productsWhere order_id=123456;
Copy after login
SUM()也可以合计计算值。
SELECT SUM(item_price*num) AS totol_price FROM order_items--返回订单中所有商品价格和。Where order_id=123456;
Copy after login

2、聚集不同值

DISTINCT关键字如下面的SQL将返回vend_id=1003的不同价格商品的平均值
SELECT AVG(DISTINCT prd_price) AS avg_price FROM productsWhere vend_id=1003;
Copy after login

3、组合聚集函数

聚集函数可以组合使用
SELECT count(*) AS num,            MIN(prod_price) AS price_min,            MAX(prod_price) AS price_max,            AVG(prod_price) AS price_avgFROM products;
Copy after login
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Use math.Max ​​function to get the maximum value in a set of numbers Use math.Max ​​function to get the maximum value in a set of numbers Jul 24, 2023 pm 01:24 PM

Use the math.Max ​​function to obtain the maximum value in a set of numbers. In mathematics and programming, it is often necessary to find the maximum value in a set of numbers. In Go language, we can use the Max function in the math package to achieve this function. This article will introduce how to use the math.Max ​​function to obtain the maximum value in a set of numbers, and provide corresponding code examples. First, we need to import the math package. In the Go language, you can use the import keyword to import a package, as shown below: import"mat

What is the word formula for average? What is the word formula for average? Sep 19, 2023 pm 01:48 PM

The average word formula is "=AVERAGE(ABOVE)". Specific steps for calculating the average: 1. Open Word and create a new document; 2. In the document, enter the data you want to calculate the average, with each data occupying one row or column. For example, you can enter data in the first column; 3. In a blank cell below or to the right of the data, open the formula bar and enter the formula "=AVERAGE(ABOVE)"; 4. Press the Enter key and Word will Calculate and display the average value.

Mean square of natural numbers? Mean square of natural numbers? Sep 20, 2023 pm 10:29 PM

The average of the squares of natural numbers is calculated by adding all the squares of n natural numbers and then dividing by that number. The first two natural numbers in the example are 2.5, 12+22=5=>5/2=2.5. There are two methods of calculation in programming - Using loops Using formulas Calculating the average of squares of natural numbers using loops This logic works by finding the squares of all natural numbers. Find the square of each by looping from 1 to n and add to the sum variable. Then divide that sum by n. Program to calculate the sum of squares of natural numbers - sample code real-time demonstration #include<stdio.h>intmain(){ intn=2;

What are the functions for averaging arrays in php? What are the functions for averaging arrays in php? Jul 17, 2023 pm 04:03 PM

PHP array averaging functions include: 1. array_sum(), which is used to calculate the sum of all values ​​in the array. In order to calculate the average, you can add all the values ​​in the array and then divide by the number of array elements; 2 , array_reduce(), used to iterate the array and calculate each value with an initial value; 3. array_mean(), used to return the average of the array, first calculate the sum of the array, and calculate the number of array elements, then The sum is divided by the number of array elements to get the average.

Get the maximum value in a sequence or set using Python's max() function Get the maximum value in a sequence or set using Python's max() function Aug 22, 2023 pm 02:10 PM

Use Python's max() function to get the maximum value in a sequence or set. In Python programming, we often need to find the largest element from a sequence or set. Python provides a built-in function max(), which can implement this function very conveniently. The max() function can accept any iterable object as a parameter, including lists, tuples, sets, etc. It returns the largest element in the passed object. The following is the basic syntax of the max() function: max(iterable[,def

How to get maximum value in PHP array How to get maximum value in PHP array Jul 07, 2023 am 11:24 AM

How to get the maximum value in a PHP array When writing PHP code, you often need to perform various operations on the array, including getting the maximum value in the array. In this article, we will introduce how to use PHP's built-in and custom functions to get the maximum value in an array, and provide corresponding code examples. Using the PHP built-in function max() PHP provides a built-in function max() that can easily get the maximum value from an array. Here is a code example using this function: &lt;?php$numbers

In C++, remove one bit of a binary number to obtain the maximum value In C++, remove one bit of a binary number to obtain the maximum value Sep 17, 2023 pm 03:53 PM

Discuss a problem given a binary number. We have to remove a little bit from it so that the remaining number should be the maximum among all other options like Input:N=1011Output:111Explanation:Weneedtoremoveonebitsoremoving0bitwillgiveamaximumnumberthanremovingany1’sbit.111>101,011.Input:111Output:11Explanation:Sinceallthebitsare1sowecanremovean

Write a code using C++ to find the number of subarrays with the same minimum and maximum values Write a code using C++ to find the number of subarrays with the same minimum and maximum values Aug 25, 2023 pm 11:33 PM

In this article, we will use C++ to solve the problem of finding the number of subarrays whose maximum and minimum values ​​are the same. The following is an example of the problem −Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2 },{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3, 1,5,

See all articles