Outside the MySQL join query (LEFT / RIGHT JOIN)

definition:

MySQL is connected to the return on a cross-connect result set of records satisfying the condition; outer first connection table and the base table is connected into the reference table, and then returns to group table based on the recording and satisfies the condition is not satisfied.

Outer join pay more attention to the relationship between the two tables. In the order of the connection table can be divided into left and right outer outside connection.

Also known as left outer left connecting, using the keyword in the FROM clause LEFT OUTER JOIN or LEFT JOIN, all rows from left to receive the keyword table (substrate table), and with the right table rows with the keyword ( reference table) rows match, i.e., matching each row from the left table and the right table qualifying rows.

Results focus on the left outer join, in addition to the matching line, further comprising a left table has the right, but does not match in table rows, such as rows for the value of the right column of the table is selected set to NULL, i.e. the left outer join result set NULL value indicates consistent with the record left table not found in the right table.

[Example 1] query tb_students_info tb_departments table and the table all students, including students do not have school, SQL statements, and input the results shown below.

Tb_students_info content table structure is as follows:

 

 Tb_departments content table structure is as follows:

 

 LEFT JOIN query results are as follows:

 

 结果显示了 12 条记录,name 为 Mona 的学生目前没有学院,因为对应的 tb_departments 表中并没有该学生的学院信息,所以该条记录只取出了 tb_students_info 表中相应的值,而从 tb_departments 表中取出的值为 NULL。

右外连接又称为右连接,在 FROM 子句中使用 RIGHT OUTER JOIN 或者 RIGHT JOIN。与左外连接相反,右外连接以右表为基表,连接方法和左外连接相同。在右外连接的结果集中,除了匹配的行外,还包括右表中有但在左表中不匹配的行,对于这样的行,从左表中选择的值被设置为 NULL。

【实例 2】在 tb_students_info 表和 tb_departments 表中查询所有学院,包括没有学生的学院,输入的 SQL 语句和执行结果如下所示。

 

 可以看到,结果只显示了 12 条记录,名称为 Music的学院目前没有学生,对应的 tb_students_info 表中并没有该学院的信息,所以该条记录只取出了 tb_departments 表中相应的值,而从 tb_students_info 表中取出的值为 NULL。

Guess you like

Origin www.cnblogs.com/ccstu/p/12175585.html