MySQL study notes ------ group function

//----------grouping function----------//

1. Grouping function

1. Function

        Used for statistics, also known as aggregation function or statistical function or group function

2. Classification

        sum summation, avg average value, max maximum value, min minimum value, count calculation number

3. Features

(1) sum and avg are generally used to deal with numeric types; max, min and count can deal with any type;

(2) The above grouping functions ignore null values;

(3) It can be combined with distinct to realize deduplication operation;

4. A separate introduction to the count function

(1) Generally, count(*) is used to count the number of rows;

(2) The fields queried together with the grouping function are required to be the fields after group by

Second, the use of grouping functions

1. Easy to use

SELECT SUM(salary) FROM employees;#一般适用于数值型
SELECT AVG(salary) FROM employees;#一般适用于数值型
SELECT MIN(salary) FROM employees;#适用于数值、字符等,即可比较的都适用
SELECT MAX(salary) FROM employees;#适用于数值、字符等,即可比较的都适用
SELECT COUNT(salary) FROM employees;#适用任何类型

SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 个数
FROM employees;

SELECT SUM(salary) 和,ROUND(AVG(salary),2) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 个数
FROM employees;

2. What types of parameters are supported

SELECT SUM(last_name) ,AVG(last_name) FROM employees;#不支持字符型
SELECT SUM(hiredate) ,AVG(hiredate) FROM employees;
SELECT MAX(last_name),MIN(last_name) FROM employees;
SELECT MAX(hiredate),MIN(hiredate) FROM employees;
SELECT COUNT(commission_pct) FROM employees;
SELECT COUNT(last_name) FROM employees;

3. Null values ​​are ignored

SELECT SUM(commission_pct) ,AVG(commission_pct),SUM(commission_pct)/35,SUM(commission_pct)/107 FROM employees;
SELECT MAX(commission_pct) ,MIN(commission_pct) FROM employees;
SELECT COUNT(commission_pct) FROM employees;
SELECT commission_pct FROM employees;

4. Collocate distinct with distinct --- the function is to remove duplicate data

SELECT SUM(DISTINCT salary),SUM(salary) FROM employees;
SELECT COUNT(DISTINCT salary),COUNT(salary) FROM employees;

5. Detailed introduction of the count function count---used to count the number of rows

SELECT COUNT(salary) FROM employees;
SELECT COUNT(*) FROM employees;
SELECT COUNT(1) FROM employees;

#效率:
#MYISAM存储引擎下,COUNT(*)的效率高
#INNODB存储引擎下,COUNT(*)和COUNT(1)的效率差不多,比COUNT(字段)要高一些

6. The fields queried together with the grouping function are limited --- AVG (salary) represents a value --- employee_id represents a column of values

SELECT AVG(salary),employee_id  FROM employees;

3. Practice

1. Query the difference in days between the maximum entry time and the minimum entry time in the employee table, DATEDIFF --- the function is the first parameter minus the second parameter

SELECT DATEDIFF(MAX(hiredate),MIN(hiredate)) FROM employees;
SELECT DATEDIFF(CURDATE(),'1995-12-3');

Guess you like

Origin blog.csdn.net/weixin_47156401/article/details/131927886