SQL join query

 It can be regarded as knowledge recycling. After work, there is no complicated link query involved. Basically, inner join is used, so that even the concepts of several links are a little vague.

  Multiple table queries can be implemented through the join operator. Connection is the main feature of the relational database model, and it is also a sign that it is different from other types of database management systems. Multi-table join query is the basic operation of using Sql, but there are many ways of connection. Skilled use of these connection methods can simplify Sql statements and improve the efficiency of database operation.

 

FROM join_table JOIN_TYPE join_table
[ON (join_condition)]

 

The join_table indicates the name of the table participating in the join operation. The join can operate on the same table or on multiple tables. The join on the same table is also called a self-join.

  join_type indicates the connection type, which can be divided into three types: inner join , outer join and cross join.

  INNER JOIN

  During inner join, only the rows that meet the query condition (WHERE search condition or HAVING condition) and join condition are returned in the query result set.

  The inner join query operation lists the rows of data that match the join condition, and it uses comparison operators to compare the column values ​​of the joined columns. There are three types of inner joins:
  1. Equijoin : use the equal sign (=) operator in the join condition to compare the column values ​​of the joined columns, and the query result lists all the columns in the joined table, including the duplicates. List.
  2. Unequal join: Use other comparison operators other than the equal operator to compare the column values ​​of the joined columns in the join condition. These operators include >, >=, <=, <, !>, !<, and <>.
  3. Natural join: The equal (=) operator is used in the join condition to compare the column values ​​of the joined columns, but it uses the select list to indicate the columns included in the query result set, and deletes duplicate columns in the join table.

  OUTER JOIN

  When an outer join is used, it returns not only the rows that meet the join conditions in the query result set, but also the left table (when left outer join), the right table (when right outer join), or two edge join tables (full outer join) connection) all data rows. The ON (join_condition) clause in a join operation indicates the join condition, which consists of columns in the joined tables and comparison operators, logical operators, and so on.

  There are three types of outer joins: left outer join (LEFT OUTER JOIN or LEFT JOIN), right outer join (RIGHT OUTER JOIN or RIGHT JOIN) and full outer join (FULL OUTER JOIN or FULL JOIN).

  1. Left outer join: The matching rows of two tables are given, and the table on the left shall prevail. If the table on the left has a row but the table on the right does not, the column selected in the corresponding row of the table on the right is displayed as NULL, allowing The benchmark table on the left corresponds to multiple records that meet the conditions in the table on the right. The left outer join is to return the matching row on the left, regardless of whether the table on the right has a corresponding row . 2. Right outer join: Given the matching rows of the two tables, And the table on the right shall prevail. If there are rows in the table on the right but not in the table on the left, the column selected in the corresponding row of the left table is displayed as NULL, allowing the benchmark table on the right to correspond to multiple records that meet the conditions in the table on the left, and the outer right table The join is to return the matching row on the right, regardless of whether the table on the left has a corresponding row
  

  3. Full outer join: Given the matching rows of two tables, the tables on both sides shall prevail. There are rows on one side but not on the other side, and the side without them is displayed as NULL.

SELECT field1,field2
FROM left_table LEFT JOIN right_table -- write on the left as the left table and on the right as the right table
ON field1 = field2 -- benchmark field, you can write multiple
WHERE left_table.field3 = right_table.field3 -- conditional statement

 

  Cross join (CROSS JOIN)
  cross join does not have a WHERE clause, it returns the Cartesian product of all data rows in the two tables to be joined, and the number of data rows returned to the result set is equal to the data that meets the query conditions in the first table The number of rows is multiplied by the number of rows of data in the second table that match the query criteria.

 

Record and forget cool!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690493&siteId=291194637