MySQL connections have outer links (left and right joins), inner joins, and cross joins (Cartesian product).
The statement to create a table based on mysql is as follows:
drop table abc,cdef; CREATE TABLE abc ( a VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'a', b VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'b', c VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'c' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='abc'; CREATE TABLE cdef ( c VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'c', d VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'd', e VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'e', f VARCHAR(20) NOT NULL DEFAULT '0' COMMENT 'f' )ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='cdef'; insert into abc (a,b,c) values ('a1','b1','c1'), ('a1','b1','c1'), ('a5','b5','c5'), ('a2','b2','c2'); insert into cdef (c,d,e,f) values ('c1','d1','e1','f1'), ('c3', 'd3', 'e3', 'f3'), ('c2','d2','e2','f2'), ('c2','d2','e2','f2');
- one. Cross join: A cross join without a where clause will result in a Cartesian product of the joined tables. The total number of records is equal to the product of the total number of data in the two tables.
-- Cartesian cross join select * from abc ,cdef;
result:
a1 b1 c1 c1 d1 e1 f1
a1 b1 c1 c1 d1 e1 f1
a5 b5 c5 c1 d1 e1 f1
a2 b2 c2 c1 d1 e1 f1
a1 b1 c1 c3 d3 e3 f3
a1 b1 c1 c3 d3 e3 f3
a5 a2 b5 c5 e3 _ _ _ b2 c2 c3 d3 e3 f3 a1
b1 c1 c2 d2 e2 f2
a1 b1 c1 c2 d2 e2 f2
a5 b5 c5 c2 d2 e2 f2
a2 b2 c2 c2 d2 e2 f2
a1 b1 c1 c2 d2 e2 f2
a1 b1 c1 c2 d2 e2 f2
a5 b2 c2 c2 f2 _ _ _ _ c2 c2 d2 e2 f2
- two. Inner joins: There are three types of inner joins: natural joins, equijoins, and non-equijoins
a. Natural join: Use the equal = operator in the join condition to compare the column values of the joined columns, but delete the duplicate columns in the join table
select * from abc natural join cdef;
c1 a1 b1 d1 e1 f1
c1 a1 b1 d1 e1 f1
c2 a2 b2 d2 e2 f2
c2 a2 b2 d2 e2 f2
b. Equi-join: Use on or where to increase the join condition equal to the = operator to compare the column values of the joined columns without deleting duplicate columns
select a.*,d.* from abc a, cdef d where a.c = d.c;a1 b1 c1c1d1 e1 f1 a1 b1 c1 c1 d1 e1 f1 a2 b2 c2 c2 d2 e2 f2 a2 b2 c2 c2 d2 e2 f2
c. Non-equivalent join: In the join condition, other comparison operators can be used to compare the column values of the joined columns, such as: <, >, !=, etc.
select a.*,d.* from abc a, cdef d where a.c != d.c;
result:
a2 b2 c2 c1 d1 e1 f1 a1 b1 c1 c3 d3 e3 f3 a1 b1 c1 c3 d3 e3 f3 a5 b5 c5 c3 d3 e3 f3 a2 b2 c2 c3 d3 e3 f3 a1 b1 c1 c2 d2 e2 f2 a1 b2 c1 e2 _ _ _ _ b5 c5 c2 d2 e2 f2 a1
b1 c1 c2 d2 e2 f2
a1 b1 c1 c2 d2 e2 f2
a5 b5 c5 c2 d2 e2 f2
- three. External links: including left join, right join
a. Left connection: The left is the main, the right is connected to the left, and the corresponding columns on the right are all null
select a.*,d.* from abc a left join cdef d on a.c = d.c;
result:
a1 b1 c1 c1 d1 e1 f1
a1 b1 c1 c1 d1 e1 f1
a5 b5 c5 (null) (null) (null) (null) a2 b2 c2 c2 d2 e2 f2 a2 b2 c2 c2 d2 e2 f2
b. Right join: The right side prevails, the left side is connected to the right side, and the corresponding columns on the left side are all null
select a.*,d.* from abc a right join cdef d on a.c = d.c;
result:
a1 b1 c1 c1 d1 e1 f1
a1 b1 c1 c1 d1 e1 f1
(zero) (zero) (zero) c3 d3 e3 f3
a2 b2 c2 c2 d2 e2 f2
a2 b2 c2 c2 d2 e2 f2