: The name of the table that requires inner join. INNER JOIN: The INNER keyword can be omitted in inner joins, and only the JOIN keyword is used.
ON clause: used to set the connection conditions of the inner join.
INNER JOIN You can also use the WHERE clause to specify connection conditions, but the INNER JOIN ... ON syntax is the official standard writing method, and the WHERE clause will affect the query at some point. performance.
When connecting multiple tables, just use INNER JOIN or JOIN continuously after FROM.
Inner joins can query two or more tables. In order to give everyone a better understanding, we will only explain the connection query between two tables for the time being.
Example
Between the tb_students_info table and the tb_course table, use inner joins to query student names and corresponding course names. The SQL statement and running results are as follows.
mysql> SELECT s.name,c.course_name FROM tb_students_info s INNER JOIN tb_course c
-> ON s.course_id = c.id;
+--------+-------------+
| name | course_name |
+--------+-------------+
| Dany | Java |
| Green | MySQL |
| Henry | Java |
| Jane | Python |
| Jim | MySQL |
| John | Go |
| Lily | Go |
| Susan | C++ |
| Thomas | C++ |
| Tom | C++ |
+--------+-------------+
10 rows in set (0.00 sec)
Copy after login
In the query statement here, the relationship between the two tables is specified through INNER JOIN, and the conditions for the connection are given using the ON clause.
Note: When querying multiple tables, you must specify which table the fields come from after the SELECT statement. Therefore, when querying multiple tables, the writing method after the SELECT statement is table name.column name. In addition, if the table name is very long, you can also set an alias for the table, so that you can write the table's alias and column name directly after the SELECT statement.
MySQL LEFT/RIGHT JOIN: Outer join query
The query results of the inner join are all records that meet the connection conditions, and the outer join will First, the connected table is divided into a base table and a reference table, and then the records that meet and do not meet the conditions are returned based on the base table.
Outer joins can be divided into left outer joins and right outer joins. The following describes left outer joins and right outer joins respectively based on examples.
Left join
Left outer join, also known as left join, uses the LEFT OUTER JOIN keyword to connect two tables, and uses the ON child Sentence to set the connection conditions.
The syntax format of left join is as follows:
SELECT <字段名> FROM <表1> LEFT OUTER JOIN <表2> <ON子句>
Copy after login
The syntax description is as follows.