[Learn MySQL from shallow to deep]- MySQL connection query

This series is: Detailed explanation of MySQL database, created exclusively for senior Java teaching teachers of Qianfeng Education

Committed to explaining the relevant knowledge points of the MySQL database to everyone, including rich code cases and explanations. If you feel that it is helpful to everyone, you can [click to follow] and continue to follow up~

At the end of the article, there are key summaries and benefits!
For technical questions, you are also welcome to communicate with us!

foreword

Starting today, this series of content will take your friends to learn database technology. Database technology is an indispensable part of knowledge content in Java development. It is also a very important technology. This series of tutorials comprehensively explains the database system from the shallower to the deeper. It is very suitable for small partners with zero foundation to learn.


The full text is about [1475] words, no nonsense, just pure dry stuff that allows you to learn techniques and understand principles! This article has a wealth of cases and pictures, so that you can better understand and use the technical concepts in the article, and can bring you enough enlightening thinking...

1. Table relationship

Friends, before learning multi-table join query , first of all, we need to introduce the relationship between the following tables . In multiple tables, there are four kinds of relationships between tables, namely: one-to-one, one-to-many, many-to-one, and many-to-many.

1. One-to-one relationship

The so-called one-to-one relationship means that in two tables, a piece of data in one table corresponds to a piece of data in the other table.

In the figure below, the ids of the student table and the student detail table have a corresponding relationship, which is a one-to-one relationship.

2. One-to-many relationship

A one-to-many relationship means that one piece of data in one table corresponds to more than N pieces of data in another table. This relationship is one-to-many.

For example, the data with tid 02 in the teacher table in the figure below corresponds to the three data items SpringCloud, Redis, and RabbitMQ in the subject chart because the value of their teaid field is 2.

3. Many-to-one relationship

The reverse view of the one-to-many relationship is many-to-one.

For example, the three pieces of data in the subject chart in the figure below SpringCloud、Redis、RabbitMQcorrespond to the data in the teacher table.

4. Many-to-many relationship

The many-to-many relationship needs to be completed with the help of an association table. As shown in the figure below, the relationship between the student table and the score table is one-to-many, and the relationship between the subject table and the score table is one-to-many. Then the relationship between the student table and the subject table is an N:N many-to-many relationship. Use the score table as an association table to complete the many-to-many relationship.

2. Table join query

Let's take everyone to learn table connection query together.

There are three types of table join queries: inner join, left outer join, and right outer join.

语法:SELECT 列名 FROM 表1 连接方式 表2 ON 连接条件

1. Inner join query (INNER JOIN ON)

Knock on the blackboard, let's draw the key points

Features: Inner join is to display the related data of two tables, and not display the data without related relationship.

#1.查询每个讲师所教的科目(显式内连接)
select * from teacher a INNER JOIN `subject` b on a.TId=b.teaid;

#2.查询每个讲师所教的科目(隐式内连接)
select * from teacher a , `subject` b where a.TId=b.teaid;

Experience: In MySql, the second method can also be used as an inner connection query, but it does not meet the SQL standard.

The first type belongs to the SQL standard and is common to other relational databases.

2. Left outer connection (LEFT JOIN ON)

Features: The left outer join is based on the left table on the left side of the LEFT JOIN keyword, and the right table on the right side of the keyword. All the data in the left table is displayed, the data in the right table is displayed normally if there is an association relationship, and null is displayed if there is no association relationship.

#查询所有学生和成绩
select * from student a LEFT JOIN score b ON a.SId = b.stuId;

Note: The left outer join is to use the left table as the main table, and then match to the right in turn, and return the result if it is matched.

If no match is found, return NULL value filling.

3. Right outer connection (RIGHT JOIN ON)

Features: The right outer join is based on the left table of the RIGHT JOIN keyword, and the right table of the keyword. All the data in the right table is displayed, the data in the left table is displayed normally if there is an association relationship, and null is displayed if there is no association relationship.

#查询所有学生和成绩
select * from student a RIGHT JOIN score b ON a.SId = b.stuId;

Note: The right outer join is based on the right table as the main table, and then matches to the left in turn, and returns the result if it is matched.

If no match is found, return NULL value filling.


3. Conclusion

Finally, here is a summary of the core points of this article:

  1. Master the four relationships between tables: one-to-one, one-to-many, many-to-one, and many-to-many.

  2. Proficiently master the three methods of table association query: inner join query, left outer join query, right outer join query, and memorize their characteristics.


Bonus benefits!

Qianfeng Education 2023 new version of MySQL course, a full set of videos from zero foundation of MySQL database from entry to mastery , click the link below to learn!
insert image description here
Video Portal:
A full set of videos from zero-basic entry to mastery of MySQL database, click to jump to the column to learn

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130641492