Preface at the beginning of writing, I thought of two beginnings. Compare the first one: several common query connection methods, and the second one: SQL (Structured Query Language) has the data definition function, data Query, data operation, and data control. What we usually call addition, deletion, modification, and query is a more concise statement of SQL functions. Check now
Preface at the beginning of writing, I thought of two beginnings. Compare the first one: several common query connection methods, and the second one: SQL (Structured Query Language) has the data definition function, data Query, data operation, and data control. What we usually call addition, deletion, modification, and query is a more concise statement of SQL functions. Check now
Preface
At the beginning of writing, I thought of two starting points and compared them.
The first entry: several common query Connection Methods of SQL
Start 2: SQL (Structured Query Language) includes the data definition function, data query function, data operation function, and data control function. What we usually call addition, deletion, modification, and query is a more concise statement of SQL functions. Now we will introduce several common connection methods.
Which one is better? I personally prefer the second type. The second reason is that it makes me clear where the knowledge I have summarized is in my knowledge network. This gives my knowledge a sense of belonging. As for why I think the second writing method is due to a course.
In the course of preparing to write this blog, Mr. Mi gave us a lesson called "re-weaving knowledge networks ". I have summarized the central idea of this course in two aspects.
First, contact us when learning new things.
Second, "If you do not want to seek the world, you cannot seek the moment. If you do not want the world, you cannot seek a domain. "The biggest achievement for me is not the two points mentioned by the teacher, but the sentence mentioned by the teacher when talking about the second point. The original words cannot be clearly understood as follows: you must have a perspective so that you can find new things that you have learned before. In this way, your learning will be much easier and happier.
I have such deep feelings about this sentence because I used to listen only to my teacher, but I actually feel "familiar strangers" to him ". I never know how to be able to have a perspective, and what is the benefit of a perspective on me. But what the teacher said was "finding", which made me clear all of a sudden. I just started to get to know you! Now I think of it, the "view of depth" will help me find out what is the focus of my new knowledge. Let me quickly understand his full picture, which laid the foundation for my next step. If there are many benefits, you need to answer more questions.
Now, let's start with the question and query several connection methods.
Connection Overview
SQL provides multiple types of connections. The difference between them is that the method used to select rows for connection is different from different overlapping data sets.
The most common connection methods are internal connection and external connection. External Connection is subdivided into left outer connection and right outer connection. In addition to these three connection modes, there are three connection modes: Full outer connection, self connection, and cross connection.
Let's take a look at the definitions of these connection methods.
Inner join: returns only rows with equal join fields in two tables.
Left outer join (left outer join): returns records that include all records in the left table and join fields in the right table.
Right outer join: returns records that include all records in the right table and the joined fields in the left table.
Full outer: returns all records in the left and right tables.
Self join: the same table is connected to itself.
Cross join: returns all rows in the left table. Each row in the left table is combined with all rows in the right table.
Simply looking at these definitions, we can have a sense of what we are talking about. Next we will use a graphic to briefly introduce them and then test them through a specific instance.
A circle represents a table. The blue circle represents the left table, and the white circle represents the right table.
From the figure, several connections are clear.
Inner join: Part C in the figure
Left Outer Join: Part A + C in the figure
Outer right join: B + C
Instance
Create two types of tables: Table_a Table_ B. add several records to the table. For example:
1. Internal Connection
-- Inner can omit innerselect a_UserID, B _StudentID from Table_ainner join Table_ B on a_UserID = B _StudentID
Result
2. left Outer Join of Outer Join
-- Left join of outer join can also be written as left join (Omitted outer) select a_UserID, B _StudentID from Table_aleft outer join Table_ B on a_UserID = B _StudentID
Result
3. Right Outer Join of Outer Join
-- Right join of outer join can also be written as right join (Omitted outer)
Select a_UserID, B _StudentID from Table_a
Right outer join Table_ B on a_UserID = B _StudentID
Result
4. All external connections
-- Select a_UserID, B _StudentID from Table_afull outer join Table_ B on a_UserID = B _StudentID
Result
5. Self-connection
-- Connect to select s. a_ID, s. a_UserName, I. a_UserID, I. a_UserName from Table_a ijoin Table_a s on I. a_UserID = s. a_ID
Result
6. Cross join
-- Cross join select I. a_UserID, t. B _StudentID from Table_a I cross join Table_ B tselect count (*) from Table_a I cross join Table_ B t
Result
Summary
This article introduces how to use frequently-used query methods and makes it easier to understand it by drawing its definition graph.