Basic related to join in sql statement (1)

For a long time, I have not spent time and energy on SQL, so for SQL statements, it is generally enough to use basic additions, deletions, and changes, but recently my colleagues encountered a SQL optimization problem. After a long time, although it took a lot of effort to get it done, I still felt that the foundation of SQL was weak, so I planned to re-learn SQL-related things, and this is the first article about join.

First of all, first throw out a graph that has been used badly, as follows:


As can be seen from the graph, joins are roughly divided into inner joins, outer joins, right joins, left joins, and natural joins.

First, create two tables, emp and dep:

CREATE TABLE `emp` (
  `id` int(11) NOT NULL,
  `empName` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `deptId` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

CREATE TABLE `dep` (
  `id` int(11) NOT NULL,
  `deptName` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `locAdd` varchar(50) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin



Insert the corresponding data:


insert into `emp` (`id`, `empName`, `deptId`) values('1','e1','1');
insert into `emp` (`id`, `empName`, `deptId`) values('2','e2','1');
insert into `emp` (`id`, `empName`, `deptId`) values('3','e3','1');
insert into `emp` (`id`, `empName`, `deptId`) values('4','e4','2');
insert into `emp` (`id`, `empName`, `deptId`) values('5','e5','2');
insert into `emp` (`id`, `empName`, `deptId`) values('6','e6','3');
insert into `emp` (`id`, `empName`, `deptId`) values('7','e7','4');
insert into `emp` (`id`, `empName`, `deptId`) values('8','e8','51');


insert into `dep` (`id`, `deptName`, `locAdd`) values('1','RD','11');
insert into `dep` (`id`, `deptName`, `locAdd`) values('2','HR','12');
insert into `dep` (`id`, `deptName`, `locAdd`) values('3','MK','13');
insert into `dep` (`id`, `deptName`, `locAdd`) values('4','MIS','14');
insert into `dep` (`id`, `deptName`, `locAdd`) values('5','FD','15');




Cartesian product: CROSS JOIN:

To understand various JOINs, you must first understand the Cartesian product. The Cartesian product is to forcibly combine each record of table A with each record of table B. So, if table A has n records and table B has m records, the result produced by the Cartesian product will produce n*m ​​records. In the example below, emp has 8 records, dep has 5 records, and the Cartesian product of all of them has 40 records. There are five ways to generate a Cartesian product as follows:

    SELECT * FROM emp CROSS JOIN dep;
    SELECT * FROM emp INNER JOIN dep;
    SELECT * FROM emp ,dep;
    SELECT * FROM emp NATURE JOIN dep;
    SELECT * FROM emp NATURA JOIN dep;


The query results are as follows:
id	empName	deptId	id	deptName	locAdd
1 e1 1 1 RD 11
1 e1 1 2 HR 12
1 e1 1 3 MK 13
1 e1 1 4 MIS 14
1	e1	1	5	FD	15
2	e2	1	1	RD	11
2	e2	1	2	HR	12
2 e2 1 3 MK 13
2	e2	1	4	MIS	14
2	e2	1	5	FD	15
3 e3 1 1 RD 11
3	e3	1	2	HR	12
3	e3	1	3	MK	13
3	e3	1	4	MIS	14
3	e3	1	5	FD	15
4 e4 2 1 RD 11
4 e4 2 2 EN 12
4 e4 2 3 MK 13
4 e4 2 4 MIS 14
4 e4 2 5 FD 15
5	e5	2	1	RD	11
5	e5	2	2	HR	12
5 e5 2 3 MK 13
5 e5 2 4 MISS 14
5 e5 2 5 FD 15
6 e6 3 1 RD 11
6	e6	3	2	HR	12
6 e6 3 3 MK 13
6 e6 3 4 MISS 14
6 e6 3 5 FD 15
7	e7	4	1	RD	11
7	e7	4	2	HR	12
7	e7	4	3	MK	13
7	e7	4	4	MIS	14
7	e7	4	5	FD	15
8	e8	51	1	RD	11
8	e8	51	2	HR	12
8	e8	51	3	MK	13
8 e8 51 4 MIS 14
8	e8	51	5	FD	15





Inner join: INNER JOIN:

Inner join INNER JOIN is the most commonly used join operation. From a mathematical point of view, it is to find the intersection of two tables. From a Cartesian product point of view, it is to pick out the records whose ON clause conditions are satisfied from the Cartesian product. There are four ways of writing INNER JOIN, WHERE (equivalent join), STRAIGHT_JOIN, JOIN (omitting INNER). Examples are as follows:

    SELECT * FROM emp INNER JOIN dep ON emp.`deptId`=dep.`id`;
    SELECT * FROM emp,dep WHERE emp.`deptId`=dep.`id`;
    SELECT * FROM emp STRAIGHT_JOIN dep ON emp.`deptId`=dep.`id`;
    SELECT * FROM emp JOIN dep ON emp.`deptId`=dep.`id`


The query results are as follows:

id	empName	deptId	id	deptName	locAdd
1 e1 1 1 RD 11
2	e2	1	1	RD	11
3 e3 1 1 RD 11
4 e4 2 2 EN 12
5	e5	2	2	HR	12
6 e6 3 3 MK 13
7	e7	4	4	MIS	14



Left join: LEFT
JOIN The meaning of LEFT JOIN is to find the intersection of two tables plus the remaining data of the left table. Still speaking from the perspective of Cartesian product, it is to first pick out the records whose ON clause conditions are satisfied from the Cartesian product, and then add the remaining records in the left table. The writing method is as follows:
SELECT * FROM emp LEFT JOIN dep ON emp.`deptId`=dep.`id`;


The query results are as follows, see the last item:

id	empName	deptId	id	deptName	locAdd
1 e1 1 1 RD 11
2	e2	1	1	RD	11
3 e3 1 1 RD 11
4 e4 2 2 EN 12
5	e5	2	2	HR	12
6 e6 3 3 MK 13
7	e7	4	4	MIS	14
8 e8 51 NULL NULL NULL



Right join: RIGHT JOIN
Similarly, right join RIGHT JOIN is to find the intersection of two tables plus the remaining data of the right table. Described again from the perspective of the Cartesian product, the right join is to pick out the records whose ON clause conditions are satisfied from the Cartesian product, and then add the remaining records in the right table. The writing method is as follows:
  SELECT * FROM emp RIGHT JOIN dep ON emp.`deptId`=dep.`id`;


The query results are as follows, see the last item:
id	empName	deptId	id	deptName	locAdd
1 e1 1 1 RD 11
2	e2	1	1	RD	11
3 e3 1 1 RD 11
4 e4 2 2 EN 12
5	e5	2	2	HR	12
6 e6 3 3 MK 13
7	e7	4	4	MIS	14
NULL	NULL	NULL	5	FD	15




Outer join: OUTER JOIN
outer join is to find the union of two sets. From the perspective of Cartesian product, it is to pick out the records whose ON clause conditions are satisfied from the Cartesian product, then add the remaining records in the left table, and finally add the remaining records in the right table. In addition, MySQL does not support OUTER JOIN, but we can implement the UNION operation on the results of left join and right join. The writing method is as follows:
SELECT * FROM emp LEFT JOIN dep ON emp.`deptId`=dep.`id`  
 
 UNION
 
 SELECT * FROM emp RIGHT JOIN dep ON emp.`deptId`=dep.`id`;



The query results are as follows:

id	empName	deptId	id	deptName	locAdd
1 e1 1 1 RD 11
2	e2	1	1	RD	11
3 e3 1 1 RD 11
4 e4 2 2 EN 12
5	e5	2	2	HR	12
6 e6 3 3 MK 13
7	e7	4	4	MIS	14
8 e8 51 NULL NULL NULL
NULL	NULL	NULL	5	FD	15




Natural join: NATURE JOIN
natural join is to find the same column in two tables as a join condition to join, written as follows:

SELECT * FROM emp NATURAL JOIN dep; 


The query results are as follows:


id	empName	deptId	deptName	locAdd
1 e1 1 RD 11
2	e2	1	HR	12
3	e3	1	MK	13
4 e4 2 MIS 14
5 e5 2 FD 15




The next article will introduce the specific sql writing methods of the picture at the beginning.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325335265&siteId=291194637