MySQL____ advanced query, union query

1. Aggregate query

1. count query (total statistics)

1.1 count usage 1

insert image description here
Recommended, the most standard, can query all null and non-null data

1.2 count usage 2

insert image description here
Can query all data (null and non-null)
Disadvantages: low compatibility

1.3 count usage 3

insert image description here
insert image description here
Count the number of non-nulls in the current field

1.4 Notes:

In different count statistics scenarios, you need to use different count queries, such as querying all data quantities (null and non-null) -> count( )
statistics in special scenarios consider using count(
) from table_name+where conditional query

is not recommended Using count(field name) is unstable

2. SUM function (sum statistics)

insert image description here

insert image description here
And statistics, if there is null, or statistics non-integer values, then its result is that only valid integer values ​​are counted
insert image description here

3.AVG function

Returns the average of the queried data, not a number meaningless

insert image description here
When avg calculates the average value, if there is any data that does not conform to the specification, such as (null), this row of data will be discarded directly

4. MAX function

insert image description here

5.MIN function

insert image description here
insert image description here

2. ifnull function

The function that determines whether it is null If the first parameter is not null, it returns the first parameter, otherwise, it returns the second parameter

Example 1

insert image description here

Example 2

insert image description here

Example 3

insert image description here

Example 4 (solving a query with a null total score)

insert image description here

3. Group query GROUP BY

3.1 Syntax

select colum1,sum(colum2),..from table group by colum2;

Query the maximum salary of each roll
insert image description here

3.2 Group condition query HAVING

having query: filter data
in group by Syntax: having is a condition after group by, and its execution order is also after group by

insert image description here

4. Joint query (multi-table query)

4.1 Pre-knowledge ---- Cartesian product

Cartesian product, also known as direct product, is expressed as X Y. For example, the data in table A is m rows, and the data in table B has n rows, then the Cartesian product of A and B is performed, and the result is m n rows.

Preparation:
build the table

-- 创建数据库
drop database if exists java33;
create database java33 default character set 'utf8mb4';

-- 切换数据库
use java33;

-- 创建班级表
drop table if exists class;
create table class(
  id int primary key auto_increment comment '班级编号',
  classname varchar(250) not null comment '班级名称'
);

-- 创建学生表
drop table if exists student;
create table student(
  id int primary key auto_increment comment '学生编号',
  sn varchar(50) comment '学号',
  username varchar(250) not null comment '学生名称',
  `mail` varchar(250) comment '邮箱',
  class_id int,
  foreign key (class_id) references class(id)
);

-- 创建课程表
drop table if exists course;
create table course(
  id int primary key auto_increment comment '课程编号',
  name varchar(250) not null
);

-- 成绩表
drop table if exists score_table;
create table score_table(
  id int primary key auto_increment comment '成绩编号',
  score decimal(4,1),
  student_id int not null,
  course_id int not null,
  foreign key (student_id) references student(id),
  foreign key (course_id) references course(id)
);
-- 班级表添加数据
insert into class(id,classname) values(1,'Java班级'),(2,'C++班级');

-- 课程表添加数据
insert into course(id,name) values(1,'计算机'),(2,'英语');

-- 学生表添加数据
insert into student(id,sn,username,mail,class_id) values(1,'CN001','张三','[email protected]',1),(2,'CN002','李四','[email protected]',2),(3,'CN003','王五','[email protected]',1);

-- 成绩表添加数据
insert into score_table(id,score,student_id,course_id) values(1,90,1,1),(2,59,1,2),(3,65,2,1),(4,NULL,2,2);

4.2 Inner connection

Inner join focuses on the commonalities between two tables, its role is to use joins to compare common data between two (or more) tables
insert image description here

4.2.1 Inner join syntax

select * from t1 [inner|cross] join t2 [on 过滤条件] [where 过滤条件]

on ------ Syntactically it can be omitted, but if it is omitted, the query is the Cartesian product of multiple tables

Two common syntaxes:

select * from t1 join t2 [on 过滤条件] [where 过滤条件]
select * from t1,t2[where 过滤条件]

4.2.2 Inner connection combat 1

Check Zhang San's results:

  1. Perform inner join query (do Cartesian product)
 select s.*,st.* from student s join score_table st;

insert image description here

  1. Remove invalid grades (on filter)
 select s.*,st.* from student s join score_table st on s.id = st.student_id;

insert image description here

  1. Query Zhang San's results (where filter condition)
    method 1
 select s.*,st.* from student s join score_table st on s.id = st.student_id where s.username = '张三';

insert image description here
Method 2

select s.*,st.* from student s cross join score_table st on s.id = st.student_id where s.username = '张三'; 

Method 3

 select s.*,st.* from student s inner join score_table st on s.id = st.student_id where s.username = '张三';

Method 4

select s.*,st.* from student s,score_table st where s.id = st.student_id and s.username = '张三';

4.3 Outer joins

Left join
insert image description here
Right
insert image description here
join The syntax of left join is as follows:

select * from t1 left join t2 on连接条件 [where 条件查询]

insert image description here

The right join syntax is as follows:

select * from t1 right join t2 on连接条件 [where 查询条件]

insert image description here
Left join and right join only need to master one syntax, because using left join can realize "right join", and only need to change the query order of the table to realize left join/right join

4.3.2 External connection in practice 1

Query everyone's personal information + course name + score information
First analyze the needs:
(1) Which tables are needed - student table, course table, score table
(2) all - use outer join main table - student table

select s.sn,s.username,s.mail,c.name,st.score from student s left join score_table st on s.id = st.student_id left join course c on c.id = st.course_id;

insert image description here

4.3.3 The difference between on and where

  1. Inner join on can be omitted, in left join and right join on cannot be omitted,
  2. Secondly, the role of on in the left/right connection is different, and the roles of on and where in the left/right connection are also different.
  3. Left join without on will report an error
  4. Note:
    In the outer join query, if there are multiple query conditions, our correct way of writing is to write all the expressions of the query conditions in the where expression instead of the on. In on, we generally only need to write one The Cartesian product filter condition for invalid data is sufficient.

4.4 Self-connection

Refers to the same table join itself to query

4.4.1 Self-connection syntax

select * from table_name as t1, table_name as t2 where t1.id = t2.id[...];

4.4.2 Inner connection combat 1

Query English score > Computer score
Analysis problem: score sheet score_name (self and oneself query)

  1. First query the subject id according to the subject name
    insert image description here
  2. self-inquiry

insert image description here

  1. Remove meaningless data in Cartesian product
    Valid data: same primary key, same student id
select * from score_table st1,score_table st2 where st1.student_id = st2.student_id;

insert image description here

  1. Set the where query condition, let table one query English scores, and let table two query computer scores
 select * from score_table st1,score_table st2 where st1.student_id = st2.student_id and st1.course_id = 2 and st2.course_id = 1;

insert image description here

  1. Set where multi-condition query, let English score > computer score
 select * from score_table st1,score_table st2 where st1.student_id = st2.student_id and st1.course_id = 2 and st2.course_id = 1 and st1.score < st2.score;

insert image description here

 select st1.score'英语成绩',st2.score'计算机成绩' from score_table st1,score_table st2 where st1.student_id = st2.student_id and st1.course_id = 2 and st2.course_id = 1 and st1.score < st2.score;

insert image description here

4.5 Subqueries (nested queries)

A subquery refers to a select statement embedded in other SQL statements, also called a nested query.

4.5.1 Subquery Practice 1

Querying Zhang San's classmates
1. Querying Zhang San's student id
insert image description here

2. Query all lists based on the class id of the previous query

select * from student where class_id=(select class_id from student where username = '张三');

insert image description here
insert image description here

4.5.2 Difference between in and =

= The query requires a specific value
in query, which can be one or more values, and satisfying any one will return true.

4.6 Combined Queries

Guess you like

Origin blog.csdn.net/biteqq/article/details/123521495