preamble
The association between multiple Mysql tables is divided into the following situations:
left join , right join , inner join , join , "," may get different results by using different association relationships. We know that the direct association of two tables may increase or decrease the number of results. Let's explain through two tables , in order to make the result more obvious, when constructing the data, there are data that are not related to each other in the two tables, that is, a certain piece of data in Table 1 is not related in Table 2, and there is a certain piece of data in Table 2 that is in Table 1 China can't connect.
text
Preparation
Create 2 tables: subject table (subject), score table (score)
create table subject
(
id int not null
primary key,
subject varchar(10) null comment '科目'
);
create table score
(
id int not null
primary key,
score int default 0 null comment '分数',
subject_id int not null comment '科目ID'
);
Insert initialization data
insert into school.subject (id, subject)
values (1, '语文'),
(2, '数学'),
(3, '英语');
insert into school.score (id, score, subject_id)
values (1, 10, 1),
(2, 10, 1),
(3, 30, 2),
(4, 50, 5);
left join
Use left join to associate 2 tables by subject ID
select subject.*,s.* from subject left join score s on subject.id = s.subject_id;
Results of the:
illustrate:
1. If the data in the left table exists but the data in the right table does not exist, then the left data will be used as the basis, and the corresponding field in the right table will be null 2. A Cartesian product will be generated. If the data on the left is associated with 2 pieces of data on the right, then there will be 2 pieces of data in the left table in the result, that is, there will be 4 pieces of data in the result, one more than the data in the left table
left join key is null
select subject.* , s.* from subject left join score s on subject.id = s.subject_id where isnull(subject_id);
Results of the:
Description: The result will filter out the data whose subject_id is not empty, so that the results can be divided into 3 categories, 1. Contains all Cartesian product results 2. Use the isnull function, including data that exists in the left table, but does not exist in the right table Data 3. Use the !isnull function to include data that exists in both the left and right tables
right join
select subject.*,s.* from subject right outer join score s on subject.id = s.subject_id;
Results of the:
illustrate:
1. If the data in the right table exists but the data in the left table does not exist, then the data in the right table will be used as the basis, and the corresponding field in the left table will be null 2. A Cartesian product will be generated. If the data in the right table is associated with the same piece of data in the left table, then the two pieces of data in the right table in the result will be associated with the same piece of data in the left table, and the two pieces of data in the result are all language data. , there are 4 pieces of data in the right table, and one piece of data does not exist in the left table, then the corresponding field of the left table is null, and the English in the left table will not appear in the result
right join key is null
select subject.* , s.* from subject right join score s on subject.id = s.subject_id where isnull(subject.subject);
Results of the:
Explanation: The data whose subject.subject is not empty in the result will be filtered out, so that the results can be divided into 3 categories,
1. Contains all Cartesian product results
2. Using the isnull function, the data contained in the right table exists, but the data in the left table does not exist
3. Use the !isnull function to include data that exists in both the left and right tables
join
select subject.*,s.* from subject join score s on subject.id = s.subject_id;
Results of the:
Description: Only the data that exists in both tables will be kept, which is equivalent to the result of "!isnull" in the above, that is, there are no null rows in the table
inner join
select subject.*,s.* from subject inner join score s on subject.id = s.subject_id;
Results of the:
Explanation: In fact, it is the way of writing Join
The two tables are associated by ","
select subject.*,s.* from subject , score s order by subject.id;
operation result:
Explanation: Cartesian product 3 * 4 = 12 pieces of data will be generated
Exchange the two tables and associate them with ","
select subject.*,s.* from score s , subject order by subject.id;
Explanation: The execution result is the same as above, indicating that it is associated with "," and has nothing to do with the left and right positions