MySQL inner connection, outer connection, (indirect) full connection

Internal connection

  • The records in the two tables that have the connection relationship between the fields that meet the connection conditions form a record set
Select A.name,B.name from A inner join B on A.id=B.id

And the following

Select A.name,B.name from A,B where A.id=B.id

The result is the same ( innerkeywords for internal connections can be omitted);

Outer join

Divided into 2 types:

  • Left outer join (based on the left table)
    • The results of the left join table A and B include all the records of A and the records of B that meet the conditions.
  • Right outer join (based on the right table)
    • The results of the right connection A and B are the same as the results of the left connection B and A

In other words:

Select A.name,B.name from A Left Join B on A.id=B.id

And the following

Select A.name,B.name from B Right Join A on B.id-A.id

The result after execution is the same.

(Indirect) Fully Connected

  • Fully connected query:是在内连接的基础上增加 左右两边没有显示的数据

  • MySQL不支持全连接And 不支持全连接 full JOINkeywords

  • If you want to be fully connected, you need to use the unionconnection 左连接和右连接to get the full connection

MySQL provides UNIONkeyword use UNIONcan 间接achieve full JOINthe function.

#查询人员和部门的所有数据
 
SELECT * FROM person LEFT JOIN dept ON person.did = dept.did
UNION
SELECT * FROM person RIGHT JOIN dept ON person.did = dept.did;

Three-table join query

#查询商店、价格、种类的所有数据

select price.id,price.store_code,store.name,price.fruit_code,category.second_name
from (price left join store on price.store_code=store.code)
	left join category on price.fruit_code=category.fruit_code

Reference

[1] Multi-table query of MySQl data query

[2] Mybatis realizes multi-table joint query

[3] The realization of mysql inner connection, outer connection and full connection

[4] MySql join (join) query (three-table left join writing)

Guess you like

Origin blog.csdn.net/weixin_43438052/article/details/114174147