Introduction to MySQL learning six

1. Multi-table query

1.1 Internal connection

Rule: Return the common records of the two tables.
Syntax:
1, select * from 表1 inner join 表2 on 表1.公共字段=表2.公共字段(Inner can be omitted)
at study entry five MySQL test data based on test data and then add the following codes:

create table stuMarks (
  examNo char(7) primary key,
  stuNo varchar(6) not null,
  writtenExam int,
  labExam int
);

insert into stumarks values ('001','01', 80, 60);
insert into stumarks values ('002','05', 90, 55);
insert into stumarks values ('003','08', 88, 89);
insert into stumarks values ('004','09', 90, 92);

Insert picture description here
Example questions:
Insert picture description here
2.select * from 表1, 表2 where 表1.公共字段=表2.公共字段;
Insert picture description here

1.2 Left outer connection

Rule: The table on the left shall prevail. If there is no corresponding record on the right, the null
syntax is displayed :select * from 表1 left join 表2 on 表1.公共字段=表2.公共字段;
Insert picture description here

1.3 Right outer connection

Rule: The table on the right shall prevail. If there is no corresponding record on the left, the null
syntax is displayed :select * from 表1 rightjoin 表2 on 表1.公共字段=表2.公共字段;
Insert picture description here

1.4 Cross connection

Syntax: select * from 表1 cross join 表2 on 条件字段;
Return the Cartesian product.
Insert picture description here
Insert picture description here
Summary:
1. If there is no connection condition, the cross connection returns to Cartesian product.
2. If there is connection condition, it is the same as inner connection

1.5 Natural connection

Automatically judge the conditional connection, the condition of the judgment is based on the field of the same name
1, natural join (natural join)
Insert picture description here
2, natural left outer join (natural left join)
Insert picture description here

3. Natural right join
Insert picture description here

Summary:
1. The table connection is connected by the field with the same name.
2. If there is no field with the same name, the Cartesian product is returned.
3. The connection value of the same name is displayed one, and the field is placed at the top

1.5.1 using

Using is used to specify connection fields. Using will also optimize public fields. The optimization rules are the same as those of natural connections.
example:
Insert picture description here

1.6 Exercise

1. Show the area and the number of people taking the test (writtenExam) in each area, and sort them in descending order:
Insert picture description here
Insert picture description here
2. Show the area where students have taken the test
Insert picture description here

3. Display the number of boys and girls.
Method one, group query:
Insert picture description here
method two, union:
Insert picture description here
method three, write conditions directly:
Insert picture description here

4. Show the number of boys, girls, and the total number of each area
Insert picture description here

Two, subquery

Syntax: The select * from 表1 where (子查询)
outer query is called the parent query, and the
child query provides query conditions for the parent query

2.1 Scalar quantum query

Features: The returned result is one.
Example: Find a student whose written test score is 88:
Insert picture description here

2.2 Column query

If the result of the scalar sub-query returns multiple records, you cannot use equals, use in or not in.
Features: The return result of the column query is one column.

1. Find students whose written test score is greater than 80:
Insert picture description here

2.3 Row subqueries

Features: The result returned by the subquery is composed of multiple fields

1. Find the boys and girls with the highest Chinese scores
Insert picture description here

2.4 Table subquery

Features: Use the results of the subquery as a table

1. Find the boys and girls with the highest Chinese scores

Insert picture description here
Note: The data source is followed by from. If you treat the subquery as a table, you must alias the result set.

2.5 exists subquery

1. If the written test score exceeds 90 points, all student information will be displayed:
Insert picture description here
2. If no one has written test scores greater than 90 points, all student information will be displayed:
Insert picture description here

On the way of learning MySQL, if you think this article is helpful to you, then please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/106978716