MySQL数据汇总与聚集函数――MySQL系列(七)_MySQL
1、聚集函数
聚集函数是运行在行组上,计算和返回单个值的函数。
(1)、AVG()函数
可以返回所有列的平均值,也可以返回特定列的平均值。SELECT AVG(prd_price) AS avg_price FROM productsWhere vend_id=1003;
(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;
SELECT SUM(item_price*num) AS totol_price FROM order_items--返回订单中所有商品价格和。Where order_id=123456;
2、聚集不同值
DISTINCT关键字如下面的SQL将返回vend_id=1003的不同价格商品的平均值SELECT AVG(DISTINCT prd_price) AS avg_price FROM productsWhere vend_id=1003;
3、组合聚集函数
聚集函数可以组合使用SELECT count(*) AS num, MIN(prod_price) AS price_min, MAX(prod_price) AS price_max, AVG(prod_price) AS price_avgFROM products;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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;

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.

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 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: <?php$numbers

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

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,
