mySQL table join

First, the connection method of mysql table

The difference between inner join and outer join:

I compare the two tables to sets A and B, where the inner join is the intersection of the set A and the set B, and the content of the intersection exists in both tables, that is, inside each table; and the outer join is In addition to the intersection, there is content in another table (left, right, full), and the result set involved is the content in both tables.

 

Second, the connection method of the table:

The connection query methods of tables include inner join, outer join (left join, right join, full join), and cross join.

 

The table used in the following example:

mysql> select * from Store_Information;

+---------------+-------+------------+

| store_name    | Sales | Date       |

+---------------+-------+------------+

| Los Angeles | 1500 | 1999-01-05 |

| Los Angeles | 500 | 1999-01-08 |

| Boston        |   700 | 1999-01-08 |

| Seven Eleven  |   860 | 1999-01-09 |

| Good Neighbor |  1100 | 1999-01-10 |

| Wumark        |  3000 | 1999-01-11 |

| Walmark | 3500 | 1998-04-15 |

| Titi | 1100 | 1999-05-15 |

+---------------+-------+------------+

8 rows in set (0.00 sec)

 

mysql> select * from Geography;

+-------------+-------------+

| region_name | store_name  |

+-------------+-------------+

| East        | Boston      |

| East        | New York    |

| West        | Los Angeles |

| West        | San Diego   |

+-------------+-------------+

4 rows in set (0.00 sec)

 

mysql>

 

1. Inner join:

The inner join query operation lists the columns of data that match the join condition, and it uses comparison operators to compare the column values ​​of the joined columns. Only the rows that match the two tables can appear in the result set, that is, the intersection of the two tables is obtained according to the conditions.

 

for example:

mysql> select * from Store_Information S,Geography G where S.store_name=G.store_name;

+-------------+-------+------------+-------------+-------------+

| store_name  | Sales | Date       | region_name | store_name  |

+-------------+-------+------------+-------------+-------------+

| Los Angeles |  1500 | 1999-01-05 | West        | Los Angeles |

| Los Angeles |   500 | 1999-01-08 | West        | Los Angeles |

| Boston      |   700 | 1999-01-08 | East        | Boston      |

+-------------+-------+------------+-------------+-------------+

3 rows in set (0.00 sec)

 

mysql> select * from Store_Information S inner join Geography G on S.store_name=G.store_name;

+-------------+-------+------------+-------------+-------------+

| store_name  | Sales | Date       | region_name | store_name  |

+-------------+-------+------------+-------------+-------------+

| Los Angeles |  1500 | 1999-01-05 | West        | Los Angeles |

| Los Angeles |   500 | 1999-01-08 | West        | Los Angeles |

| Boston      |   700 | 1999-01-08 | East        | Boston      |

+-------------+-------+------------+-------------+-------------+

3 rows in set (0.00 sec)

 

mysql> select * from Store_Information S inner join Geography G using(store_name);

+-------------+-------+------------+-------------+

| store_name  | Sales | Date       | region_name |

+-------------+-------+------------+-------------+

| Los Angeles |  1500 | 1999-01-05 | West        |

| Los Angeles |   500 | 1999-01-08 | West        |

| Boston      |   700 | 1999-01-08 | East        |

+-------------+-------+------------+-------------+

3 rows in set (0.00 sec)

 

mysql>

 

The first sql and the second sql statement above are equivalent, and the third statement is the short form of the second sql, the replacement pattern of the on clause and the using clause. The using clause is no longer demonstrated below.

 

2. Outer join:

The result set returned to the query contains not only the rows that meet the join conditions, but also all the rows in the left table (in the case of a left outer join), the right table (in the case of a right outer join), or the left and right tables (full outer join).

 

Left outer join:

mysql> select * from Store_Information S left join Geography G on S.store_name=G.store_name;

+---------------+-------+------------+-------------+-------------+

| store_name    | Sales | Date       | region_name | store_name  |

+---------------+-------+------------+-------------+-------------+

| Los Angeles   |  1500 | 1999-01-05 | West        | Los Angeles |

| Los Angeles   |   500 | 1999-01-08 | West        | Los Angeles |

| Boston        |   700 | 1999-01-08 | East        | Boston      |

| Seven Eleven  |   860 | 1999-01-09 | NULL        | NULL        |

| Good Neighbor |  1100 | 1999-01-10 | NULL        | NULL        |

| Wumark | 3000 | 1999-01-11 | NULL | NULL |

| Walmark       |  3500 | 1998-04-15 | NULL        | NULL        |

| Titi          |  1100 | 1999-05-15 | NULL        | NULL        |

+---------------+-------+------------+-------------+-------------+

8 rows in set (0.00 sec)

 

mysql>

 

Right outer join:

mysql> select * from Store_Information S right join Geography G on S.store_name=G.store_name;

+-------------+-------+------------+-------------+-------------+

| store_name  | Sales | Date       | region_name | store_name  |

+-------------+-------+------------+-------------+-------------+

| Boston      |   700 | 1999-01-08 | East        | Boston      |

| NULL        |  NULL | NULL       | East        | New York    |

| Los Angeles |  1500 | 1999-01-05 | West        | Los Angeles |

| Los Angeles |   500 | 1999-01-08 | West        | Los Angeles |

| NULL        |  NULL | NULL       | West        | San Diego   |

+-------------+-------+------------+-------------+-------------+

5 rows in set (0.00 sec)

 

mysql>

 

Full connection (full connection is not supported in mysql, we can use left and right merge to solve):

mysql> select * from Store_Information S left join Geography G on S.store_name=G.store_name union (select * from Store_Information S right join Geography G on S.store_name=G.store_name);

+---------------+-------+------------+-------------+-------------+

| store_name    | Sales | Date       | region_name | store_name  |

+---------------+-------+------------+-------------+-------------+

| Los Angeles   |  1500 | 1999-01-05 | West        | Los Angeles |

| Los Angeles   |   500 | 1999-01-08 | West        | Los Angeles |

| Boston        |   700 | 1999-01-08 | East        | Boston      |

| Seven Eleven  |   860 | 1999-01-09 | NULL        | NULL        |

| Good Neighbor |  1100 | 1999-01-10 | NULL        | NULL        |

| Wumark | 3000 | 1999-01-11 | NULL | NULL |

| Walmark       |  3500 | 1998-04-15 | NULL        | NULL        |

| Titi          |  1100 | 1999-05-15 | NULL        | NULL        |

| NULL          |  NULL | NULL       | East        | New York    |

| NULL          |  NULL | NULL       | West        | San Diego   |

+---------------+-------+------------+-------------+-------------+

10 rows in set (0.00 sec)

 

mysql>

 

3. Cross-connect:

 

The cross join does not have a where clause, it returns the Cartesian product of all data lines in the two tables to be connected, and the number of data rows returned to the result set is equal to the data lines in the first table that meet the query conditions multiplied by the second The number of rows of data in the table that match the query criteria.

mysql> select * from Store_Information S cross join Geography G order by S.store_name;

+---------------+-------+------------+-------------+-------------+

| store_name    | Sales | Date       | region_name | store_name  |

+---------------+-------+------------+-------------+-------------+

| Boston        |   700 | 1999-01-08 | West        | Los Angeles |

| Boston        |   700 | 1999-01-08 | East        | New York    |

| Boston        |   700 | 1999-01-08 | East        | Boston      |

| Boston        |   700 | 1999-01-08 | West        | San Diego   |

| Good Neighbor |  1100 | 1999-01-10 | West        | Los Angeles |

| Good Neighbor |  1100 | 1999-01-10 | East        | New York    |

| Good Neighbor |  1100 | 1999-01-10 | East        | Boston      |

| Good Neighbor |  1100 | 1999-01-10 | West        | San Diego   |

| Los Angeles   |  1500 | 1999-01-05 | West        | Los Angeles |

| Los Angeles   |   500 | 1999-01-08 | West        | Los Angeles |

| Los Angeles   |  1500 | 1999-01-05 | East        | New York    |

| Los Angeles   |   500 | 1999-01-08 | East        | New York    |

| Los Angeles   |  1500 | 1999-01-05 | East        | Boston      |

| Los Angeles   |   500 | 1999-01-08 | East        | Boston      |

| Los Angeles   |  1500 | 1999-01-05 | West        | San Diego   |

| Los Angeles   |   500 | 1999-01-08 | West        | San Diego   |

| Seven Eleven  |   860 | 1999-01-09 | West        | Los Angeles |

| Seven Eleven  |   860 | 1999-01-09 | East        | New York    |

| Seven Eleven  |   860 | 1999-01-09 | East        | Boston      |

| Seven Eleven  |   860 | 1999-01-09 | West        | San Diego   |

| Titi          |  1100 | 1999-05-15 | West        | Los Angeles |

| Titi          |  1100 | 1999-05-15 | East        | New York    |

| Titi          |  1100 | 1999-05-15 | East        | Boston      |

| Titi | 1100 | 1999-05-15 | West | San Diego |

| Walmark       |  3500 | 1998-04-15 | West        | Los Angeles |

| Walmark       |  3500 | 1998-04-15 | East        | New York    |

| Walmark       |  3500 | 1998-04-15 | East        | Boston      |

| Walmark       |  3500 | 1998-04-15 | West        | San Diego   |

| Wumark        |  3000 | 1999-01-11 | West        | Los Angeles |

| Wumark        |  3000 | 1999-01-11 | East        | New York    |

| Wumark        |  3000 | 1999-01-11 | East        | Boston      |

| Wumark        |  3000 | 1999-01-11 | West        | San Diego   |

+---------------+-------+------------+-------------+-------------+

32 rows in set (0.00 sec)

 

mysql>

A natural join is an equijoin with duplicate attributes removed.

The differences and connections between the two are as follows:

1. Natural joins must be equijoins, but equijoins are not necessarily natural joins. Equijoin does not remove duplicate attributes; while natural join removes duplicate attributes.

2. Equijoin requires equal components, not necessarily common attributes; while natural join requires equal components must be common attributes.

 

http://blog.163.com/girl_lihuiyue@126/blog/static/1806962120142141165441/

It is relatively simple to write:

http://huaxia524151.iteye.com/blog/1423614

Guess you like

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