[MySQL series] Inner join and outer join of tables learning

The content of the "Preface" article is roughly the inner connection and outer connection to the MySQL table.

"Belonging column" MySQL

"Homepage link" personal homepage

"Author" Mr. Maple Leaf (fy)

MySQL

1. Inner connection

The inner join is actually to use the where clause to filter the Cartesian product formed by the two tables. The queries learned in the previous chapters are all inner joins
, which are also the most used join queries in the development process.

The inner join syntax is as follows:

SELECT ... FROM t1 INNER JOIN t2 ON 连接条件 [INNER JOIN t3 ON 连接条件] ... AND 其他条件;

illustrate:

  • Uppercase indicates keywords, and [ ] indicates optional items
  • The condition of the inner connection is specified by the connection condition, and other filter conditions of the user are specified by other conditions

Display SMITH's first and department names

According to the previous practice, take the Cartesian product of the employee table and the department table, specify the filter condition in the where clause as the department number of the employee is equal to the department number of the department, filter out the matching department information for each employee, and specify the employee name for SMITH

mysql> select ename, dname from emp, dept
    -> where emp.deptno = dept.deptno and ename='SMITH';

insert image description here
The essence of the above multi-table query method is an inner join. The following uses standard inner join SQL to write (the writing method is different):

  • Put the employee table and department number in the from clause and inner joinseparate them by keywords
  • After onthe clause, indicate that the inner connection condition is that the department number of the employee is equal to the department number of the department, so as to ensure that the filtered data is meaningful
  • After andspecifying the filter criteria as the employee's name is SMITH
mysql> select ename, dname from emp inner join dept 
    -> on emp.deptno = dept.deptno and ename='SMITH';

insert image description here

2. Outer connection

Outer joins are divided into left outer joins and right outer joins

2.1 Left Outer Join

If you perform a joint query, the table on the left is completely displayed, we say it is a left outer join, and the data on the right is not completely displayed or is NULL

The syntax is as follows:

SELECT ... FROM t1 LEFT JOIN t2 ON 连接条件 [LEFT JOIN t3 ON 连接条件] ... AND 其他条件;

illustrate:

  • Written represents keywords, and [ ] represents optional items
  • The condition of the left outer join is specified by the join condition, and the other filter conditions of the user are specified by other conditions

For example, create two tables

-- 建两张表
create table stu (id int, name varchar(30)); -- 学生表
insert into stu values(1,'jack'),(2,'tom'),(3,'kity'),(4,'nono');
create table exam (id int, grade int); -- 成绩表
insert into exam values(1, 56),(2,76),(11, 8);

insert image description here

Query the grades of all students. If the student has no grades, the student's personal information will also be displayed

The topic requires that the personal information of students who have no grades should also be displayed, that is to say, the contents of the student table need to be fully displayed.

If the student table is placed on the left when joining the student table and the grade table, then you can use a left outer join

If a record in the left table does not find a matching record in the right table according to the join condition, the corresponding column information in the right table will be filled with NULL values

mysql> select * from stu left join exam on stu.id=exam.id;

insert image description here

2.2 Right outer join

If the query is combined, the table on the right is completely displayed, we say it is a right outer join, and the data on the left is not completely displayed or is NULL

The syntax is as follows:

SELECT ... FROM t1 RIGHT JOIN t2 ON 连接条件 [RIGHT JOIN t3 ON 连接条件] ... AND 其他条件;

illustrate:

  • Uppercase indicates keywords, and [ ] indicates optional items
  • The condition of the right outer join is specified by the join condition, and the other filter conditions of the user are specified by other conditions

Jointly query the stu table and the exam table to display all the grades, even if there is no student corresponding to the grade, it must be displayed

The title requires that there are no students whose corresponding grade information should also be displayed, that is, the content in the grade table needs to be completely displayed

If you put the grade table on the right side when joining the student table and the grade table, you can use the right outer join

A record in the right table does not find a matching record in the left table according to the join condition, and the corresponding information in the left table will be filled with NULL values

mysql> select * from stu right join exam on stu.id=exam.id;

insert image description here

Exercise: List department names and employee information for those departments, and also list departments without employees

A database of employee information is used
insert image description here

The topic requires that the department name and employee information be listed at the same time, so the department table and employee table need to be connected

The topic requires that departments without employees be listed at the same time, that is, the contents of the department table need to be fully displayed. If the department table is placed on the left when connecting the department table and employee table, then the left outer join can be used

mysql> select dname, emp.* from dept left join emp on dept.deptno=emp.deptno;

insert image description here
It can also be operated by right outer join. Right outer join is to display the table on the complete right, that is, the department table

mysql> select dname, emp.* from emp right join dept on dept.deptno=emp.deptno;

insert image description here
--------------------- END ----------------------

「 作者 」 枫叶先生
「 更新 」 2023.8.26
「 声明 」 余之才疏学浅,故所撰文疏漏难免,
          或有谬误或不准确之处,敬请读者批评指正。

Guess you like

Origin blog.csdn.net/m0_64280701/article/details/132502524