Four types of SQL connections - left outer join, right outer join, inner join, full join

The join condition can be specified in the FROM or WHERE clause, it is recommended to specify the join condition in the FROM clause. The WHERE and HAVING clauses can also contain search conditions to further filter the rows selected by the join condition. 
Joins can be divided into the following categories:

1. Inner joins (typical join operations, using comparison operators like = or <>). Including equal joins and natural joins.  
Inner joins use comparison operators to match rows in two tables based on the values ​​of columns common to each table. For example, retrieve all rows in the students and courses tables with the same student ID.

2. Outer join. An outer join can be a left outer join, a right outer join, or a full outer join. 
When specifying an outer join in the FROM clause, it can be specified by one of the following sets of keywords:

1) The result set of a LEFT JOIN or LEFT OUTER JOIN 
includes all the rows of the left table specified in the LEFT OUTER clause, not just the rows matched by the joined columns. If a row in the left table has no matching row in the right table, then all select list columns in the right table are null in the associated result set row.

2) RIGHT JOIN or RIGHT OUTER JOIN 
right outer join is the reverse join of left outer join. will return all rows of the right table. If a row of the right table has no matching row in the left table, a null value will be returned for the left table. 
3) FULL JOIN or FULL OUTER JOIN 
full outer join returns all rows in the left and right tables. When a row has no matching rows in another table, the select list column of the other table contains null values. If there are matching rows between the tables, the entire result set row contains the data values ​​of the base table.

3. Cross join 
Cross join returns all rows in the left table, each row in the left table combined with all rows in the right table. Cross joins are also known as Cartesian products.

The tables or views in the FROM clause can be specified in any order by an inner join or a full outer join; however, when specifying tables or views with a left or right outer join, the order of the tables or views is important. For more information about arranging tables using left or right outer joins, see Using Outer Joins.

a table:

id name
1 Zhang San
2 Li Si
3 Wang Wu


table b:

id job parent_id
1 23 1
2 34 2
3 34 4


a.id has a relationship with parent_id

1) The inner join 
select a.*,b.* from a inner join b on a.id=b.parent_id 
result is

1   张3                    1     23     1   
2   李四                   2      34     2       

2) The left join 
select a.*,b.* from a left join b on a.id=b.parent_id 
result is

  1   张3                   1     23     1   
  2   李四                  2     34     2   
  3   王武                  null   

3) 
select a.*,b.* from a right join b on a.id=b.parent_id 
The result of the right join is

1   张3                   1     23     1   
2   李四                  2     34     2   
null                       3     34     4   

4) The full connection 
select a.*,b.* from a full join b on a.id=b.parent_id 
result is

1   张3                  1     23     1   
2   李四                 2     34     2   
null                   3     34     4   
3   王武                 null

Reference: http://www.blogjava.net/zolly/archive/2007/10/23/SQLJION.html

Guess you like

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