Learn MySQL with teacher Wang: aggregate functions
Teacher: Wang Shaohua QQ Group No.: 483773664 Learning Content
Usage of COUNT (), SUM (), AVG (), MAX (), and Min () four aggregate functions
The aggregate functions include count (), SUM (), AVG (), MAX (), and Min ().
One, COUNT ()
(a) Introduction
The count () function is used to count the number of records.
(ii) examples
Use the count () function to count the number of records for an employee table
1 |
SELECT COUNT (*) FROM employee ; |
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/83/E3/wKiom1d_MrnCYqpxAAAKsk8GQgo370.png " >
Second, SUM () function (i.) INTRODUCTION
The sum () function is the SUM function. Use the sum () function to calculate the sum of a field value in a table.
(ii) Example 1 data preparation
1234567891011121314151617181920 |
CREATE
TABLE
grade(
num
INT
(10)
NOT NULL ,
course
VARCHAR
(10)
NOT NULL ,
score
FLOAT
);
INSERT INTO grade
VALUES
(1001,
‘数学‘
,80);
INSERT INTO grade
VALUES
(1001,
‘语文‘
,90);
INSERT INTO grade
VALUES
(1001,
‘英语‘
,85);
INSERT INTO grade
VALUES
(1001,
‘计算机‘
,95);
INSERT INTO grade
VALUES
(1002,
‘数学‘
,88);
INSERT INTO grade
VALUES
(1002,
‘语文‘
,90);
INSERT INTO grade
VALUES
(1002,
‘英语‘
,89);
INSERT INTO grade
VALUES
(1002,
‘计算机‘
,90);
INSERT INTO grade
VALUES
(1003,
‘数学‘
,80);
INSERT INTO grade
VALUES
(1003,
‘语文‘
,98);
INSERT INTO grade
VALUES
(1003,
‘英语‘
,85);
INSERT INTO grade
VALUES
(1003,
‘计算机‘
,95);
|
2 Student's total
Use the sum () function to count the total number of students in the grade table, which is 1001.
1 |
select num, sum (score) from grade where num=1001; |
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/83/E3/wKiom1d_MrmwmG2tAAAKJlIRH3Y619.png " >
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/83/E2/wKioL1d_MrnQnVUKAAAPzVmY_0g931.png " >
The result of a manual total is the same as using the SUM function.
3. Attention
The sum () function can only calculate fields of numeric types. such as int, float, double, decimal, and so on.
Third, AVG () function
(a) Introduction
The AVG () function is a function of averaging
(ii) examples
Use the AVG () function to calculate the average age in the employee table
1 |
select avg (age) from employee; |
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/83/E2/wKioL1d_MrqipQK0AAAIEuJhnMc507.png " >
Four, Max () functions
(a) Introduction
Use the max () function to find the maximum value of a field in a table.
(ii) Example 1, using the max () function to query the maximum age in the employee table
1 |
select max(age) from employee; |
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/83/E3/wKiom1d_MrqRW8LRAAAH9zYJGhg312.png " >
2. The max () function applies not only to numeric types, but also to string types
The principle of comparison: The ASCII code is calculated, that is, the word line a minimum, the letter Z maximum
Comparison method: Compare the first letter first, if the first letter is the same, and then continue to compare the next letter.
Data preparation
12345678910 |
CREATE TABLE work
(
id
INT PRIMARY KEY UNIQUE
,
name VARCHAR
(10),
sex
VARCHAR
(4),
info TEXT
); INSERT INTO work VALUES
(1001,
‘hjh‘
,
NULL
,
NULL
);
INSERT INTO work VALUES
(1002,
‘cch‘
,
NULL
,
NULL
);
INSERT INTO work VALUES
(1003,
‘zk‘
,
NULL
,
‘student‘
);
|
Use the max () function to query the maximum value of the Name field in the work table
1 |
select max ( name ) from work ; |
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02
Learn MySQL with teacher Wang: aggregate functions