MySQL multi-table query

cross connect

A cross join looks like this:

select * from t1 cross join t2;
OR
select * from t1,t2;

Each row in the t1 table is associated with any row in the t2 table, and vice versa. This result is the same as the "Cartesian product".

inner join

The query result of an inner join is a combination of two tables that satisfy a certain condition at the same time. If no query condition is added, the query result of the inner join and the query result of the cross join are the same.

select * from t1 inner join t2 on t1.id = t2.id;
OR
select * from t1 join t2 on t1.id = t2.id;
OR
select * from t1 inner join t2 where t1.id = t2.id;
OR
select * from t1,t2 where t1.id = t2.id;

Use on or where to specify query conditions, which is equivalent to the intersection of two tables that meet a certain condition.

If you add a query condition to a cross join, the result is the same as that of an inner join, but in the general SQL standard, the two are different.

Inner joins can be divided into equijoins and unequal joins. The previous example shows equijoins. The form of unequal joins is as follows:

select * from t1 inner join t2 on t1.id > t2.id;

The result of the query is more than "equi-join".

Inner join also has a self-join, the table it joins is itself, as follows:

select * from t1 t11 inner join t1 t12 on t11.id = t12._id;
OR
select * from t1 t11, t1 t12 where t11.id = t12._id;

Query one table as two tables by specifying different fields.

outer join

Outer joins are divided into left outer joins and right outer joins. The left outer join queries more data than the inner join under the same conditions.

select * from t1 t11 left outer join t1 t12 on t11.id = t12._id;
OR
select * from t1 t11 left join t1 t12 on t11.id = t12._id;

The left outer join will not only query the combination of records that meet the conditions in the two tables at the same time, but also display the records that do not meet the conditions in the table on the left side of the "left outer join". Since this part of the records in the table on the left does not meet the join conditions, this part of the records is connected with empty records.

That is, in the left outer join, all records in the left table will be queried, the records that meet the conditions will be combined with the records that meet the conditions in the right table, and the records that do not meet the conditions will be combined with the empty records in the right table. connect.

Similar to the left outer join, the right outer join displays all the records in the right table, the records that meet the conditions will be combined with the records that meet the conditions in the left table, and the records that do not meet the conditions will be combined with the records on the left. Empty records join in the table.

select * from t1 t11 right outer join t1 t12 on t11.id = t12._id;
OR
select * from t1 t11 right join t1 t12 on t11.id = t12._id;

After using the outer join to query the data, you can also use the where clause to filter the query results, as shown below:

select * from t1 t11 right join t1 t12 on t11.id = t12._id where t11.id is not null;

Filter out records whose t11.id is null.

joint query

Union query (union) is to display the query results of multiple query statements together.

select * from t1 union select * from t2;

The data in the t2 table will be displayed after the t1 table, so the number of fields queried by the two select statements must be the same.

If there are duplicate data in the two tables, they will be merged into one record.

When using union all to query, all duplicate records will be displayed.

fully connected

In a full join, as long as a table matches the condition, records are returned with null as the result of the missing match. MySQL does not directly support the "full join" keyword, but "full join" can be achieved through the combination of "left join", "union" and "right join".

select * from t1 t11 left join t1 t12 on t11.id = t12._id;
union
select * from t1 t11 right join t1 t12 on t11.id = t12._id;

The queried data contains any row that matches the conditions in the t1 and t2 tables.

For the queried data, you can also use the where clause to further filter.

Guess you like

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