: The name of the table that requires cross-connection. WHERE clause: used to set the query conditions for cross connections.
Note: When multiple tables are cross-connected, CROSS JOIN or , can be used continuously after FROM. The return results of the above two syntaxes are the same, but the first syntax is the officially recommended standard writing method.
When there is no relationship between the connected tables, we will omit the WHERE clause. At this time, the returned result is the Cartesian product of the two tables, and the number of returned results is the multiplication of the data rows of the two tables. It should be noted that if each table has 1000 rows, then the number of returned results will be 1000×1000 = 1000000 rows, and the amount of data is very huge.
Cross connection can query two or more tables. In order to give readers a better understanding, the following will first explain the cross connection query of two tables.
Example
Query the student information table and subject information table, and get a Cartesian product.
In order to facilitate the observation of the running results after the cross connection between the student information table and the subject table, we first query the data of these two tables separately, and then perform the cross connection query.
1) Query the data in the tb_students_info table. The SQL statement and running results are as follows:
mysql> SELECT * FROM tb_students_info;
+----+--------+------+------+--------+-----------+
| id | name | age | sex | height | course_id |
+----+--------+------+------+--------+-----------+
| 1 | Dany | 25 | 男 | 160 | 1 |
| 2 | Green | 23 | 男 | 158 | 2 |
| 3 | Henry | 23 | 女 | 185 | 1 |
| 4 | Jane | 22 | 男 | 162 | 3 |
| 5 | Jim | 24 | 女 | 175 | 2 |
| 6 | John | 21 | 女 | 172 | 4 |
| 7 | Lily | 22 | 男 | 165 | 4 |
| 8 | Susan | 23 | 男 | 170 | 5 |
| 9 | Thomas | 22 | 女 | 178 | 5 |
| 10 | Tom | 23 | 女 | 165 | 5 |
+----+--------+------+------+--------+-----------+
10 rows in set (0.00 sec)
Copy after login
2) Query the data in the tb_course table. The SQL statement and running results are as follows:
mysql> SELECT * FROM tb_course;
+----+-------------+
| id | course_name |
+----+-------------+
| 1 | Java |
| 2 | MySQL |
| 3 | Python |
| 4 | Go |
| 5 | C++ |
+----+-------------+
5 rows in set (0.00 sec)
Copy after login
3) Use CROSS JOIN to query the Cartesian product of the two tables. The SQL statement and running results are as follows:
mysql> SELECT * FROM tb_course CROSS JOIN tb_students_info;
+----+-------------+----+--------+------+------+--------+-----------+
| id | course_name | id | name | age | sex | height | course_id |
+----+-------------+----+--------+------+------+--------+-----------+
| 1 | Java | 1 | Dany | 25 | 男 | 160 | 1 |
| 2 | MySQL | 1 | Dany | 25 | 男 | 160 | 1 |
| 3 | Python | 1 | Dany | 25 | 男 | 160 | 1 |
| 4 | Go | 1 | Dany | 25 | 男 | 160 | 1 |
| 5 | C++ | 1 | Dany | 25 | 男 | 160 | 1 |
| 1 | Java | 2 | Green | 23 | 男 | 158 | 2 |
| 2 | MySQL | 2 | Green | 23 | 男 | 158 | 2 |
| 3 | Python | 2 | Green | 23 | 男 | 158 | 2 |
| 4 | Go | 2 | Green | 23 | 男 | 158 | 2 |
| 5 | C++ | 2 | Green | 23 | 男 | 158 | 2 |
| 1 | Java | 3 | Henry | 23 | 女 | 185 | 1 |
| 2 | MySQL | 3 | Henry | 23 | 女 | 185 | 1 |
| 3 | Python | 3 | Henry | 23 | 女 | 185 | 1 |
| 4 | Go | 3 | Henry | 23 | 女 | 185 | 1 |
| 5 | C++ | 3 | Henry | 23 | 女 | 185 | 1 |
| 1 | Java | 4 | Jane | 22 | 男 | 162 | 3 |
| 2 | MySQL | 4 | Jane | 22 | 男 | 162 | 3 |
| 3 | Python | 4 | Jane | 22 | 男 | 162 | 3 |
| 4 | Go | 4 | Jane | 22 | 男 | 162 | 3 |
| 5 | C++ | 4 | Jane | 22 | 男 | 162 | 3 |
| 1 | Java | 5 | Jim | 24 | 女 | 175 | 2 |
| 2 | MySQL | 5 | Jim | 24 | 女 | 175 | 2 |
| 3 | Python | 5 | Jim | 24 | 女 | 175 | 2 |
| 4 | Go | 5 | Jim | 24 | 女 | 175 | 2 |
| 5 | C++ | 5 | Jim | 24 | 女 | 175 | 2 |
| 1 | Java | 6 | John | 21 | 女 | 172 | 4 |
| 2 | MySQL | 6 | John | 21 | 女 | 172 | 4 |
| 3 | Python | 6 | John | 21 | 女 | 172 | 4 |
| 4 | Go | 6 | John | 21 | 女 | 172 | 4 |
| 5 | C++ | 6 | John | 21 | 女 | 172 | 4 |
| 1 | Java | 7 | Lily | 22 | 男 | 165 | 4 |
| 2 | MySQL | 7 | Lily | 22 | 男 | 165 | 4 |
| 3 | Python | 7 | Lily | 22 | 男 | 165 | 4 |
| 4 | Go | 7 | Lily | 22 | 男 | 165 | 4 |
| 5 | C++ | 7 | Lily | 22 | 男 | 165 | 4 |
| 1 | Java | 8 | Susan | 23 | 男 | 170 | 5 |
| 2 | MySQL | 8 | Susan | 23 | 男 | 170 | 5 |
| 3 | Python | 8 | Susan | 23 | 男 | 170 | 5 |
| 4 | Go | 8 | Susan | 23 | 男 | 170 | 5 |
| 5 | C++ | 8 | Susan | 23 | 男 | 170 | 5 |
| 1 | Java | 9 | Thomas | 22 | 女 | 178 | 5 |
| 2 | MySQL | 9 | Thomas | 22 | 女 | 178 | 5 |
| 3 | Python | 9 | Thomas | 22 | 女 | 178 | 5 |
| 4 | Go | 9 | Thomas | 22 | 女 | 178 | 5 |
| 5 | C++ | 9 | Thomas | 22 | 女 | 178 | 5 |
| 1 | Java | 10 | Tom | 23 | 女 | 165 | 5 |
| 2 | MySQL | 10 | Tom | 23 | 女 | 165 | 5 |
| 3 | Python | 10 | Tom | 23 | 女 | 165 | 5 |
| 4 | Go | 10 | Tom | 23 | 女 | 165 | 5 |
| 5 | C++ | 10 | Tom | 23 | 女 | 165 | 5 |
+----+-------------+----+--------+------+------+--------+-----------+
50 rows in set (0.00 sec)
Copy after login
It can be seen from the running results that after the cross-join query of the tb_course and tb_students_info tables, 50 items were returned Record. As you can imagine, when there is a lot of data in the table, the running results obtained will be very long, and the running results obtained are not very meaningful. Therefore, this method of multi-table query through cross connection is not commonly used, and we should try to avoid this kind of query.
Cartesian product
The Cartesian product refers to the product of two sets X and Y.
For example, there are two sets A and B, and their values are as follows:
A = {1,2}
B = {3,4,5}
Copy after login
The result sets of sets A×B and B×A are respectively expressed as:
A×B={(1,3), (1,4), (1,5), (2,3), (2,4), (2,5) };
B×A={(3,1), (3,2), (4,1), (4,2), (5,1), (5,2) };
Copy after login
The above results of A×B and B×A are called the Cartesian product of the two sets.
And, from the above results, we can see that:
The multiplication of two sets does not satisfy the exchange rate, that is, A×B≠B×A.
The Cartesian product of set A and set B is the number of elements of set A × the number of elements of set B.
The algorithm followed by multi-table queries is the Cartesian product mentioned above. The connection between tables can be regarded as a multiplication operation. In practical applications, the use of Cartesian product should be avoided because there is a large amount of unreasonable data in the Cartesian product. Simply put, it can easily lead to duplicate and confusing query results.
Inner JOIN
Inner JOIN combines records in two tables and returns related fields by setting connection conditions. Matching records, that is, the intersection (shaded) part of the two tables is returned.
Inner join uses the INNER JOIN keyword to connect two tables, and uses the ON clause to set the connection conditions. Without join conditions, INNER JOIN and CROSS JOIN are syntactically equivalent and interchangeable.
The syntax format of inner connection is as follows:
SELECT <字段名> FROM <表1> INNER JOIN <表2> [ON子句]
Copy after login
The syntax description is as follows.