当前位置:网站首页>There are three kinds of SQL connections: internal connection, external connection and cross connection

There are three kinds of SQL connections: internal connection, external connection and cross connection

2022-07-05 06:18:00 Ostrich5yw


Prepare two sheets and PersonId Related to .
 Insert picture description here

One 、 Cross connect (CROSS JOIN)

Cross connect returns the Cartesian product of all data rows of the connected two tables .

//  These two sentences sql Completely equivalent 
select * from person CROSS JOIN address
select * from person, address

Query results :
 Insert picture description here

Two 、 Internal connection (INNER JOIN)

The internal connection can obtain the records of the common part of the two tables , That is to use conditional expressions to eliminate some data rows of cross connection .

//  These two sentences sql Completely equivalent 
select * from person INNER JOIN address ON person.PersonId = address.PersonId 
select * from person, address WHERE person.PersonId = address.PersonId 

Query results : Notice here PersonId = 4 My little grandson didn't export
Therefore, the inner connection only queries the data contained in both tables
 Insert picture description here

3、 ... and 、 External connection (LEFT JOIN、RIGHT JOIN、FULL JOIN)

Be careful : When connecting outside , Attention should be paid to where And on The difference between

  • on The condition is the condition used to generate the temporary table , It doesn't care on Is the condition in true , Will return to the records in the table on the left .
  • where The condition is that after the temporary table is generated , Then filter the temporary table . There is no such thing as left join The meaning of ( You have to go back to the records in the table on the left ) 了 , If the conditions are not true, filter them out

1. The left outer join

The result set of the left out join includes all rows of the specified left table , Not just the rows that the join columns match . If a row in the left table does not match a row in the right table , All selection list columns in the right table in the associated result set row are null (null)

select * from person LEFT JOIN address ON person.PersonId = address.PersonId 

Query results : Notice here PersonId = 4 Of ‘ Little grandson ’ Also output and due to ‘ Little grandson ’ There is no corresponding address, therefore address Data set to null
 Insert picture description here

2. Right connection

The right outer join is the reverse join of the left outer join . All rows of the right table will be returned . If a row in the right table does not match a row in the left table , Null value will be returned for the left table (null)

select * from person RIGHT JOIN address ON person.PersonId = address.PersonId 

Query results : Notice here AddressId = 6 Of ‘ guangdong ’ Also output and due to ‘ guangdong ’ There is no corresponding person, therefore preson Data set to null
 Insert picture description here

3. Complete external connection

Full outer join returns all rows in the left and right tables . When a row has no matching row in another table , The selection list column of another table contains null values (null)
Mysql China does not support it. FULL JOIN, So here we use UNION Joint query instead of

select * from person LEFT JOIN address ON person.PersonId = address.PersonId 
UNION
select * from person RIGHT JOIN address ON person.PersonId = address.PersonId 

Query results : At the same time, it outputs ' Little grandson ' as well as ' guangdong '
 Insert picture description here

原网站

版权声明
本文为[Ostrich5yw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050615250590.html