Join query (multi-table)

Inner join query:

Is the most commonly used queries, has become equivalent to a query , that is, in the Cartesian product data records in the table relations , table relationships to retain data in all matches, and discard data does not match.

Matching condition can be divided according to the natural connection, connections and equivalent connections are not equivalent.

(1) equivalent connections (inner join)

Conditions used to connect two tables referred to as connection conditions. If the connection condition = operator is connected, called the equivalent connection.

(2) natural join (natural join)

Natural join operation is the Cartesian product table relationships, first matching record in the field according to the relation table of the same name, then remove the duplicate fields. Is also understood to be equivalent in the target column connection property kind of repeating column was removed natural join.

(3) Unequal connection (inner join)

Where the words in the conditions used to connect two tables referred to as connection conditions. If the connection condition = operator is connected, called the equivalent connection. If other operator, it is not connected to the equivalent

External link inquiry

An outer connector can query two or more tables, and query external connection connector is very similar to the query, but also need to be connected by a designated field, when the field values ​​are equal, the recording can query the table; and, this field is not equal to the value of the recording can also check out.

External connection can be divided into left and right connections.
The basic syntax is as follows:

select 字段表 from1 LEFT | RIGHT [ OUTER ] JOIN2 ON1.字段=2.字段

(1) left outer connector (left join)

It contains a record of all the left side of the table and even the right of the table and it does not match the record.

(2) connected to the right outer (left join)

Record contains all of the right side of the table and even left the table and it does not match the record

Subqueries

Sometimes, when querying, the conditions required is another result of a select statement, this time, it is necessary to use sub-queries.

By sub-queries, queries between multiple tables can be achieved. Subquery may include IN, NOT IN, ANY, EXISTS and NOT EXISTS keywords like. Subquery may also contain comparison operators, such as "=", "! =", ">" And "<" and the like.

Subqueries (1) with the keyword IN

IN keyword based on whether the value of a field in a specified set.
Here Insert Picture Description

(2) sub-queries EXISTS keyword

keyword indicates that there exists, use the keyword exists, within the query does not return records query.
Here Insert Picture Description
Here Insert Picture Description

Subquery (3) with ANY keyword

ANY keyword indicates that any one condition is met.
Here Insert Picture Description

Subquery (4) with the keyword ALL

ALL keyword indicates that all conditions are fulfilled.
Here Insert Picture Description

skill


In a specific application, if necessary multi-table queries of data records,
generally do not use a join query, because the operation efficiency is relatively low,
thus providing an alternative and MySQL join query operation - the sub-queries.

Published 141 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42554191/article/details/103979555
Recommended