mysql left connection right connection and inner connection

  • 2020-05-09 19:24:17
  • OfStack

The script is as follows:
drop table table1;
CREATE TABLE `andrew`.`table1`
(
`name` VARCHAR(32) NOT NULL,
`city` VARCHAR(32) NOT NULL
)
ENGINE = MyISAM;
insert into TABLE1(name, city) values ('Person A', 'BJ');
insert into TABLE1(name, city) values ('Person B', 'BJ');
insert into TABLE1(name, city) values ('Person C', 'SH');
insert into TABLE1(name, city) values ('Person D', 'SZ');
commit;
drop table table2;
CREATE TABLE `andrew`.`table2`
(
`name` VARCHAR(32) NOT NULL,
`city` VARCHAR(32) NOT NULL
)
ENGINE = MyISAM;
insert into TABLE2(name, city) values ('Person W', 'BJ');
insert into TABLE2(name, city) values ('Person X', 'SH');
insert into TABLE2(name, city) values ('Person Y', 'SH');
insert into TABLE2(name, city) values ('Person Z', 'NJ');
commit;
1. External connection and left connection results

table1 is on the left, so it is called the left connection. In this case, table1 dominates, meaning that all records in table1 are listed. There are 1, 3 cases:
a. For each record in table1, if the city also happens to exist in table2 and there happens to be only one, then it will be in a1
A new record is formed in the result returned. As shown above, Person A and Person B correspond to each other.
b. If the city corresponding to each record in table1 also happens to exist in table2 and has an N bar, then a new N bar will be formed in the returned result. The situation corresponding to Person C above.
c. If the city corresponding to each record in table1 does not exist in table2, then one is formed in the returned result
A new record, and all NULL to the right of that record. The situation corresponding to Person D above.
Records that do not conform to the three rules above will not be listed.
2. External connection and right connection results

table2 is on the right, so it is called right connection. In this case, table2 dominates, meaning that all records in table2 are listed. There are 1, 3 cases:
a. For each record in table2, if the city also happens to exist in table1 and there happens to be only one, then it will be in a2
A new record is formed in the result returned. As shown above, Person X and Person Y correspond to each other.
b. For each record in table2, if the city also happens to exist in table1 and has an N bar, then N new records will be formed in the returned result. Person W corresponds to the situation above.
c. For every record in table2 that does not exist in table1, a city is formed in the returned result
A new record, all NULL to the left of the record. The above situation corresponds to Person Z.
Records that do not conform to the three rules above will not be listed.
3. The inner connection

There is no such thing as an NULL field in an internally connected data record. You can simply assume that the result of an inner link is the result of removing the record with the field NULL from the result of a left or right join. It can even be argued that if only the data records obtained after inner join operation are left in two tables respectively, such as Person A, Person B and Person C in table1, and Person W, Person X and Person Y in table2, then the return result of the left join and the right join between the two tables is 1.
Note: select * from a table b on b city a a a b a city = The effect of b.city is 1, that is, if there is no keyword such as left, right, or inner to the left of join, the default is inner join. Also, MySQL does not support full join.

Related articles: