Home > Database > Mysql Tutorial > body text

Sharing of data table connection methods in MySQL

PHPz
Release: 2023-06-15 21:09:31
Original
4142 people have browsed it

MySQL is a server-side relational database management system that helps manage large amounts of data and can also operate on data in a variety of ways. The most important of these is connecting different data tables. In MySQL, there are several different join methods that can be used to join data tables, each with different advantages and disadvantages. This article shares some common MySQL data table connection methods, hoping to help readers better understand and use MySQL.

1. Left join

In the left join, we return all the information of the left table and the matching data in the right table. If there is no matching data in the table on the right, a NULL value is returned.

Syntax:

SELECT left_table.column_name(s), right_table.column_name(s)
FROM left_table LEFT JOIN right_table
ON left_table.column_name = right_table.column_name;

Example:

Suppose there are two tables, namely the student table (student) and the family table (family). The student table contains the student's name and age, and the family table contains the corresponding name of the student. father's name.

Use left join to connect these two tables. The SQL statement is as follows:

SELECT student.name, student.age, family.father_name
FROM student
LEFT JOIN family
ON student.name = family.student_name;

Here, you can get the name, age and father's name. If there is no matching father's name for a student, a NULL value is returned here.

2. Right join

The right join can be regarded as the mirror version of the left join. In the right join, we return all the information from the table on the right and the matching data from the table on the left. If there is no matching data in the left table, a NULL value is returned.

Syntax:

SELECT left_table.column_name(s), right_table.column_name(s)
FROM left_table RIGHT JOIN right_table
ON left_table.column_name = right_table.column_name;

Example:

Still using the above example of student table and family table, use right join to connect these two tables. The SQL statement is as follows:

SELECT student.name, student .age, family.father_name
FROM student
RIGHT JOIN family
ON student.name = family.student_name;

Here we get name, age and father’s name. If there is no matching student name, a NULL value is returned here.

3. Inner join

Inner join only returns data with a matching relationship in the two tables. If there is no matching relationship between the two tables, no data will be returned.

Syntax:

SELECT left_table.column_name(s), right_table.column_name(s)
FROM left_table
INNER JOIN right_table
ON left_table.column_name = right_table.column_name ;

Example:

Use inner join to connect student table and family table. The SQL statement is as follows:

SELECT student.name, student.age, family.father_name
FROM student
INNER JOIN family
ON student.name = family.student_name;

Here, we only obtain the association between the two tables of student name, age and father’s name data.

4. Outer joins

Outer joins are the general term for left joins, right joins and complete outer joins. In the outer join, we return all the data from the two tables and match them together. If only one table exists in a certain data but not in another table, NULL will be returned in its corresponding data position.

Syntax:

SELECT left_table.column_name(s), right_table.column_name(s)
FROM left_table
FULL OUTER JOIN right_table
ON left_table.column_name = right_table. column_name;

Example:

Use external joins to connect the student table and the family table. The SQL statement is as follows:

SELECT student.name, student.age, family.father_name
FROM student
FULL OUTER JOIN family
ON student.name = family.student_name;

Here we get the data of all student and family tables and put them together. If the father is found directly in the family table but the corresponding student is not found, a NULL value is returned here.

5. Self-joining

Self-joining refers to connecting two different items in a single table as two tables. Usually this method is used to find some corresponding information in the table.

Syntax:

SELECT a.column_name(s), b.column_name(s)
FROM table a, table b
WHERE a.column_name = b.column_name AND a. column_name = 'value';

Example:

Suppose there is a table containing countries and their capitals. We can use self-joining methods to query the connections between all capitals of the country.

Use self-join to implement the above query. The SQL statement is as follows:

SELECT a.country, a.capital as capital_city, b.capital
FROM capital_table a, capital_table b
WHERE a .capital != b.capital AND a.country = b.country;

Here, you can get the associated data between each country and the capitals of other countries.

Summary:

MySQL provides several different table connection methods, each method can be used to handle different data connection requirements. By gaining an in-depth understanding of these methods and their usage, we can handle data connections in MySQL more efficiently and gain a better grasp of the use of MySQL.

The above is the detailed content of Sharing of data table connection methods in MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template