oracle--left join and 和left join where的区别

When the development process, often encounter left join, inner join statement, Join one of the important operating relational database systems, relatively faster, and therefore we usually prefer join statement.

     But in doing procedures, for some uses of the join is not necessarily very clear. Today everyone is talking about and left join and left join where.

     When returned to the database record by connecting two or more tables, generates an intermediate temporary table, and then returns this to the user the temporary table.

     When using the left jion on, and where the difference and conditions are as follows:

     1, on condition that the conditions used in the generation of a temporary table, regardless of whether it is on the condition is true, will return the records to the left of the table, and will filter out records B table. Table B ineligible portion are all set to null.

     2, where the condition is good after the temporary table generation, then the temporary table of filter conditions. At this time has no meaning left join in (must return the records left of the table), and the condition is not really all filtered out. 



 

    Example:

Construction of the table statement:

      create table tmp_lxq_1
(
  id varchar2(10),
  name varchar2(20)
);
insert   into tmp_lxq_1
select '1','张三' from dual;
insert   into tmp_lxq_1
select '2','李四' from dual;
insert   into tmp_lxq_1
select '3','王五' from dual;
commit;

tmp_lxq_1 table results:

NAME ID
. 1 Zhang
2 John Doe
3 Wangwu

drop table tmp_lxq_2;
create table tmp_lxq_2
(
  id varchar2(10),
  subject varchar2(30),
  score varchar2(30)
);

insert   into tmp_lxq_2
select '1','语文','80' from dual;
insert   into tmp_lxq_2
select '2','数学','90' from dual;
insert   into tmp_lxq_2
select '4','英语','60' from dual;
commit;

tmp_lxq_2 results:

SUBJECT SCORE ID
1 Language 80
MATHEMATICAL 90
4 60 English

Then run the following several statements:

1.inner join

inner jion take the intersection of Table A and Table B, either A or B in the left table table on the left.

 

select a.id,a.name,b.id,b.subject,b.score from tmp_lxq_1 a
inner join tmp_lxq_2 b
on a.id=b.id;

result:

SUBJECT the SCORE NAME ID ID
1 1 languages seating 80
2 90 John Doe Mathematics 2

 

2.left join 

A record is left join table based, A can be seen as the left table, B can be seen at right, left join the subject table is left. In other words, the left table (A) of the record will be fully represented, and the right table (B) show only the records that meet the search criteria (for example: A.ID = B.ID). Lack of local B table records are NULL.

 

select a.id,a.name,b.id,b.subject,b.score from tmp_lxq_1 a
left join tmp_lxq_2 b
on a.id=b.id;

Joe Smith Language 1 1 80
2 2 John Doe mathematics 90
And the king five   


3.left join and

left join and are recorded in Table A based, A can be seen as the left table, B can be seen at right, left join and is subject to the left of the table. In other words, the left table (A) of the record will be fully represented, and the right table (B) show only the records that meet the search criteria (for example: A.ID = B.ID). Lack of local B table records are NULL, the plus and conditions, A table records will all be represented, while the B table will only matching records are displayed, B table records that do not meet the conditions of the place showed It is null.

 

select a.id,a.name,b.id,b.subject,b.score from tmp_lxq_1 a
left join tmp_lxq_2 b
on a.id=b.id
and b.score>=80;

NAME SUBJECT SCORE ID ID
1 1 Joe Smith Language 80
2 John Doe 2 Mathematics 90
And the king five   


4.left join where 

left join where the condition is good after the temporary table generation, then the temporary table of filter conditions. At this time has no meaning left join in (must return the records left of the table), and the condition is not really all filtered out. 

 

select a.id,a.name,b.id,b.subject,b.score from tmp_lxq_1 a
left join tmp_lxq_2 b
on a.id=b.id
where b.score>=80;

SUBJECT the SCORE NAME ID ID
1 1 languages seating 80
2 90 John Doe Mathematics 2

Published 316 original articles · won praise 33 · views 210 000 +

Guess you like

Origin blog.csdn.net/yz18931904/article/details/105253870