Oracle中left join,right join,inner join分析

Before starting the analysis, we first prepare some test data. First, initialize three tables, namely test_user, test_team, and test_type. The table creation statement is as follows

create table TEST_USER
(
  user_id   NUMBER,
  user_name VARCHAR2(30 CHAR),
  user_team NUMBER,
  user_type VARCHAR2(30 CHAR)
);


create table test_team(
team_id number,
team_name varchar2(30char)
);


create table test_type(
type_id number,
type_name varchar2(30char)
);


Initialization data

insert into TEST_USER (USER_ID, USER_NAME, USER_TEAM, USER_TYPE)
values (1, 'user1', 1, '1');
 
insert into TEST_USER (USER_ID, USER_NAME, USER_TEAM, USER_TYPE)
values (2, 'user2', 2, null);
 
insert into TEST_USER (USER_ID, USER_NAME, USER_TEAM, USER_TYPE)
values (3, 'user3', 5, '2');
 
insert into TEST_USER (USER_ID, USER_NAME, USER_TEAM, USER_TYPE)
values (4, 'user4', null, '3');
 
insert into TEST_USER (USER_ID, USER_NAME, USER_TEAM, USER_TYPE)
values (5, 'user5', 3, '6');


 
 

insert into TEST_TEAM (TEAM_ID, TEAM_NAME)
values (1, 'team1');
 
insert into TEST_TEAM (TEAM_ID, TEAM_NAME)
values (2, 'team2');
 
insert into TEST_TEAM (TEAM_ID, TEAM_NAME)
values (3, 'team3');
 
insert into TEST_TEAM (TEAM_ID, TEAM_NAME)
values (4, 'team4');


 
 

insert into TEST_TYPE (TYPE_ID, TYPE_NAME)
values (1, 'type1');
 
insert into TEST_TYPE (TYPE_ID, TYPE_NAME)
values (2, 'type2');
 
insert into TEST_TYPE (TYPE_ID, TYPE_NAME)
values (3, 'type3');


The data of the three tables are as follows
Table: test_user


Table: test_team

表:test_type

1. Left join left join 
  left join is to query all the data in the left table of left join, the specific usage, such as

select * from test_user a left join test_team b on a.user_team=b.team_id


The query results are as follows


Here in the left join statement, all records in the table on the left side, that is, the test_user table, will be queried. The association condition following the on keyword should be understood as a matching condition. For the table on the right side of the left join, that is, the test_team table that meets the matching condition, the corresponding The record information of the table on the right, and the record of the corresponding field in the table on the right that does not meet the matching conditions is displayed as NULL.

Let's look at another sql below

select * from test_user a left join test_team b on a.user_team=b.team_id and b.team_id=1


The query results are as follows

From the above description, we can see that only the first record of the table test_user and test_team meets the matching conditions of a.user_team=b.team_id and b.team_id=1


Other test_team corresponding fields that do not meet the matching conditions are displayed as NULL.

We continue to query a sql

select * from test_user a left join test_team b on a.user_team=b.team_id and a.user_id=2


The query results are as follows


Same as above, the table test_user and test_team meet the matching conditions of a.user_team=b.team_id and a.user_id=2 only the first record, and the corresponding fields of test_team that do not meet the matching conditions are displayed as NULL.

About the difference between on condition and where

We will sql

select * from test_user a left join test_team b on a.user_team=b.team_id and b.team_id=1


And should be where

select * from test_user a left join test_team b on a.user_team=b.team_id where b.team_id=1


The query results are as follows

The where here should understand the filter conditions, the query will first form a temporary table that meets the requirements according to the matching condition a.user_team=b.team_id after on, and then the condition b.team_id=1 after where to eliminate records that do not meet the conditions.

Similarly, another rewritten sql

select * from test_user a left join test_team b on a.user_team=b.team_id where a.user_id=2


The query results are as follows

(+) in oracle

 In oracle, the left join can be used to join the left table, add (+) to the side of the relationship, said,

For example, left join sql

select * from test_user a left join test_team b on a.user_team=b.team_id


Rewritten into (+) form, there are two ways as follows, the effect is the same

select * from test_user a,test_team b where  a.user_team=b.team_id(+);


or

select * from test_user a,test_team b where b.team_id(+)=a.user_team;


The performance of left join and (+) expression in oracle is the same in execution. The corresponding execution plans are as follows:


with

There is another noteworthy place here, such as sql 

select * from test_user a left join test_team b on a.user_team=b.team_id and b.team_id=1

How to rewrite it as (+)?

Is that so?

This rewrite is wrong, as can be seen from the query results. Because where is followed by the filter condition, in

select * from test_user a left join test_team b on a.user_team=b.team_id and b.team_id=1

In b.team_id=1, the matching condition after on does not affect the number of returned results, and the results will not be eliminated according to the conditions. Here put b.team_id=1 in where is the filter condition.

The correct way to rewrite, should be in accordance with the above emphasized left join left join can use the left join table, add (+) on the side of its association relationship (in fact, right join right join is also the same), that is, rewrite as follows

 

select * from test_user a,test_team b where a.user_team=b.team_id(+) and b.team_id(+)=1;


The query results are as follows


Two, right join right join

Right join is to query the data in the left table of right join, the specific usage, such as

 

select * from test_type a right join test_user b on a.type_id=b.user_type


The query results are as follows


Here in the right join statement, all records in the table on the right side, that is, the test_user table, will be queried. The association condition following the on keyword should be understood as a matching condition. For the table on the left side of the right join, that is, the test_type table that meets the matching condition, it will be displayed.

Shows the record information corresponding to the table on the right, and records that do not meet the matching conditions in the corresponding field of the table on the left are displayed as NULL.

The right join can be compared to the left join, including using (+) to rewrite in oracle, but (+) should be added to the side of the relationship on the left side of the right join.

Three, inner join inner join

If you say right join, left join is to take a table to match another table, matching the information corresponding to the information, otherwise it returns NULL, and the inner join is equivalent to two tables matching each other, whichever is the intersection. That is equivalent to the = connection after where.

So the two sql below are equivalent.

with

Guess you like

Origin blog.csdn.net/qq_39999478/article/details/106348615