Happy to take you to learn the MySQL database Part 6

Insert image description here

Insert query results

Combine the query and the new addition.
Use the query results as new data.
Example: Insert the query results of the student1 table into the student2 table as new data.

 create table student1(id int, name varchar(20));
 create table student2(id int, name varchar(20));
 insert into student1 values(1, '张三'), (2, '李四'), (3, '王五');
 insert into student2 select * from student1;

Constraint requirements:

  • The number of columns and type obtained from the query result must match the number of columns and type of the inserted table => the table structure is the same

Aggregation query

During the query process, certain operations are performed between rows in the table.
Dependence on aggregate functions => library functions provided by SQL.
Common aggregate functions

function illustrate
COUNT([DISTINCT] expr) Return the number of queried data => the number of rows in the query result
SUM([DISTINCT] expr) Returns the sum of the queried data, which is meaningless if it is not a number => Sum
AVG([DISTINCT] expr) Returns the average value of the queried data, which is meaningless if it is not a number => average value
MAX([DISTINCT] expr) Returns the maximum value of the queried data, which is meaningless if it is not a number => maximum value
MIN([DISTINCT] expr) Returns the minimum value of the queried data, which is meaningless if it is not a number => minimum value

count => Calculate the number of result rows obtained by pressing select * from table name.
This operation can be understood as executing select * first, and then calculating how many rows there are in the select * query result.

create table student(id int, name varchar(20));
insert into student values(1, '张三'), (2, '李四'), (3, '王五'), (4, NULL);
select count(*) from student;

sum=> is only valid for numeric sub-columns. If it is a string column, it cannot be calculated.
Find the total Chinese scores of all students in a class.

-- 创建考试成绩表
drop table if exists exam_result;
create table exam_result (
	id int,
	name varchar(20),
	chinese decimal(3,1),
	math decimal(3,1),
	english decimal(3,1)
);

-- 插入测试数据
insert into exam_result (id, name, chinese, math, english) values
	(1,'唐三藏', 67, 98, 56),
	(2,'孙悟空', 87.5, 78, 77),
	(3,'猪悟能', 88, 98.0, 90),
	(4,'曹孟德', 82, 84, 67),
	(5,'刘玄德', 55.5, 85, 45),
	(6,'孙权', 70, 73, 78.5),
	(7,'宋公明', 75, 65, 30);

-- 求全班语文成绩和
select sum(chinese) from exam_result;

-- 往表中插入含有null的一行数据
insert into exam_result values (8, '如来佛主', null, null, null);
select sum(chinese) from exam_result;
-- 数据结果没变,正常显示

The sum operation will automatically skip the rows with null results. avg
calculates the Chinese average.
image-20230908113008248
Aggregation functions can also be used with expressions
to find the average of the total scores
image-20230908115329333
and find the highest and lowest Chinese scores in the class.
image-20230908121114106

~~SQL has certain "statistical calculation" capabilities! It's just like Excel => Just because SQL has such statistical capabilities, sometimes SQL is also a skill that non-technical positions (product managers) need to master. Can it be improved
? Put another aggregate function inside the aggregate function?
Answer: (1) The ability of SQL to express logic is limited. If there is such a need, you can use Java to operate SQL. Complex logic is expressed in Java. SQL only does simple queries and statistics. => Common practices ⭐️ ⭐️ ⭐️
(2) In fact, complex sql can also be written, but the readability and execution efficiency will be very poor.

These aggregation functions, by default, aggregate all classes in this table.
Sometimes, group aggregation is also needed (records are divided into several groups according to the specified fields. Each group uses an aggregation function separately)

-- 创建员工表
create table employee(id int, name varchar(20), post varchar(20), salary int);

-- 往表中插入几条记录
insert into employee values (1, '初一', '讲师', 10000);
insert into employee values (2, '初二', '讲师', 11000);
insert into employee values (3, '初三', '讲师', 12000);
insert into employee values (4, '初四', '学管师', 10000);
insert into employee values (5, '初五', '学管师', 9000);
insert into employee values (6, '如来佛祖', '老板', 100000);
insert into employee values (7, '太上老君', '老板', 120000);

To find the average salary of each position
~~you need to use group query
~~use group by to group
. Specify a column and put the same values ​​in the column into the same group.
image-20230908155901036
image-20230908155752335

The column specified by select must either have an aggregate function, or it must be a column of the specified group by... You cannot specify a non-aggregation, non-group by column~~For example, write name, and the name is not included in the column that is not group by. Aggregation function, the query results here are meaningless at this time!!! (no error will be reported)

When grouping, you can specify conditions for filtering.
~~First figure out whether the filtering condition is before grouping or after grouping.
Find the average salary of each position except Sun Wukong
1. Before grouping, filter and use where condition.
Find each The average salary of the position, but excluding the first-year students
image-20230908162814632
2. After grouping, filter, use the having condition
to find the average salary of each position, but excluding the boss
image-20230908163212437
3. You can filter before and after the grouping at the same time!!
Find the average of each position Salary, but excluding the positions of first-year junior high school classmates and bosses
image-20230908163940500

Union query (multi-table query)

Join multiple tables together for query
~~Cartesian product (an operation). The joint query is expanded based on this operation
~~The plane rectangular coordinate system we are familiar with is also called the Cartesian coordinate system Cartesian product, which is actually A kind of permutation and combination. Arrange and combine the records of the two tables as much as possible to create N situations

Guess you like

Origin blog.csdn.net/m0_73740682/article/details/132763992