Oracle connect the left and right connection, full outer join and sign (+) use the left Oracle connection, right join, full outer join and sign (+) Usage

Oracle connect the left and right connection, full outer join and sign (+) Usage

 

1, ready to work

Oracle outer connector (OUTER JOIN) comprises the following:

  • Left outer connector (left table without restriction)
  • Right outer join (right table without restriction)
  • Full outer join (approximately two tables are no restrictions)

The corresponding SQL: LEFT / RIGHT / FULL OUTER JOIN. OUTER keyword is usually omitted, wrote: LEFT / RIGHT / FULL JOIN.

When the left and right connections are in a table based on Table A, the entire contents of the table will be displayed, and then add the contents of Table A and Table B match. If the data is not recorded in the Table A Table B. Then column shows the result set associated with the null value (NULL).

For external connection, you may be used "(+)" to represent. On the use of (+) Some notes:

  1. (+) Operator can only occur in the WHERE clause, and can not be used simultaneously with the syntax OUTER JOIN.
  2.  When the operator performs the outer connector, included in the WHERE clause if there are multiple conditions, the conditions must be included in all the (+) using (+) operator.
  3. (+) Operator applies only to the column, rather than using expression on.
  4. (+) Operator and can not be used with IN OR operator.
  5. (+) Operator is only used for the outer left and right outer connector, while not fully achieved for the outer connector.

Create two tables, insert data for learning tests:

Copy the code
CREATE TABLE t_A (
id   number,
name  VARCHAR2(10)
);

CREATE TABLE t_B (
id   number,
name  VARCHAR2(10)
);

INSERT INTO t_A VALUES(1,'A');
INSERT INTO t_A VALUES(2,'B');
INSERT INTO t_A VALUES(3,'C');
INSERT INTO t_A VALUES(4,'D');
INSERT INTO t_A VALUES(5,'E');

INSERT INTO t_B VALUES(1,'AA');
INSERT INTO t_B VALUES(1,'BB');
INSERT INTO t_B VALUES(2,'CC');
INSERT INTO t_B VALUES(1,'DD');
Copy the code

  

2、左外连接(LEFT OUTER JOIN/ LEFT JOIN)

  LEFT JOIN of the table is recorded based on the left, can be seen as an example the left table t_A, t_b can be seen as the right table, the result set is that all the data in the table t_A, t_A Coupled with the table and the table matches t_b The data. In other words, the left table (t_A) will record all of that out, but right table (t_B) will only show records that match the search criteria. Insufficient t_B table where records are NULL.

select * from t_A a left join t_B b on a.id = b.id;
或
select * from t_A a left outer join t_B b on a.id = b.id;

  

  (+) To achieve with this number can be understood +: + means to add, that there is a plus sign which table, this table is to match the table. If the plus sign written on the right table, the table is to show all the left, it is left connected.

Select * from t_A a,t_B b where a.id=b.id(+);

  

3, right outer join (RIGHT OUTER JOIN / RIGHT JOIN)

  LEFT JOIN and the results just the opposite, is the right table (t_B) basis. It is the result set list all records t_B, t_A plus data and t_B match. Insufficient t_A table where records are NULL.

select * from t_A a right join t_B b on a.id = b.id;
或
select * from t_A a right outer join t_B b on a.id = b.id;

  

  (+) To achieve with this number can be understood +: + means to add, that there is a plus sign which table, this table is to match the table. If the plus sign written on the left table, right table is to show all, it is the right connection.

Select * from t_A a,t_B b where a.id(+)=b.id;

  

4, the whole outer connector (FULL OUTER JOIN / FULL JOIN)

     Left and right tables do not limit, all the records have shown that less than two tables where are NULL. External connection does not support a full (+) writing.

select * from t_A a full join t_B b on a.id = b.id;
或
select * from t_A a full outer join t_B b on a.id = b.id;    

  

补充

select * from t_A a,t_B b where a.id = b.id;
select * from t_A a join t_B b on a.id = b.id;

  

select * from t_A a where a.id in (select b.id from t_B b);
select * from t_A a where exists (select 1 from t_B b where a.id = b.id);

  

本博客内容主要以学习、研究和分享为主,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

1、准备工作

Oracle  外连接(OUTER JOIN)包括以下:

  • 左外连接(左边的表不加限制)
  • 右外连接(右边的表不加限制)
  • 全外连接(左右两表都不加限制)

对应SQL:LEFT/RIGHT/FULL OUTER JOIN。 通常省略OUTER关键字, 写成:LEFT/RIGHT/FULL JOIN。

在左连接和右连接时都会以一张A表为基础表,该表的内容会全部显示,然后加上A表和B表匹配的内容。 如果A表的数据在B表中没有记录。 那么在相关联的结果集行中列显示为空值(NULL)。

对于外连接, 也可以使用“(+) ”来表示。 关于使用(+)的一些注意事项:

  1. (+)操作符只能出现在WHERE子句中,并且不能与OUTER JOIN语法同时使用。
  2.  当使用(+)操作符执行外连接时,如果在WHERE子句中包含有多个条件,则必须在所有条件中都包含(+)操作符。
  3. (+)操作符只适用于列,而不能用在表达式上。
  4. (+)操作符不能与OR和IN操作符一起使用。
  5. (+)操作符只能用于实现左外连接和右外连接,而不能用于实现完全外连接。

创建两张表,插入数据用于学习测试:

Copy the code
CREATE TABLE t_A (
id   number,
name  VARCHAR2(10)
);

CREATE TABLE t_B (
id   number,
name  VARCHAR2(10)
);

INSERT INTO t_A VALUES(1,'A');
INSERT INTO t_A VALUES(2,'B');
INSERT INTO t_A VALUES(3,'C');
INSERT INTO t_A VALUES(4,'D');
INSERT INTO t_A VALUES(5,'E');

INSERT INTO t_B VALUES(1,'AA');
INSERT INTO t_B VALUES(1,'BB');
INSERT INTO t_B VALUES(2,'CC');
INSERT INTO t_B VALUES(1,'DD');
Copy the code

  

2、左外连接(LEFT OUTER JOIN/ LEFT JOIN)

  LEFT JOIN是以左表的记录为基础的,示例中t_A可以看成左表,t_B可以看成右表,它的结果集是t_A表中的全部数据,再加上t_A表和t_B表匹配后的数据。换句话说,左表(t_A)的记录将会全部表示出来,而右表(t_B)只会显示符合搜索条件的记录。t_B表记录不足的地方均为NULL。

select * from t_A a left join t_B b on a.id = b.id;
或
select * from t_A a left outer join t_B b on a.id = b.id;

  

  用(+)来实现, 这个+号可以这样来理解: + 表示补充,即哪个表有加号,这个表就是匹配表。如果加号写在右表,左表就是全部显示,所以是左连接。

Select * from t_A a,t_B b where a.id=b.id(+);

  

3、右外连接(RIGHT OUTER JOIN/RIGHT JOIN)

  和LEFT JOIN的结果刚好相反,是以右表(t_B)为基础的。它的结果集是t_B表所有记录,再加上t_A和t_B匹配后的数据。 t_A表记录不足的地方均为NULL。

select * from t_A a right join t_B b on a.id = b.id;
或
select * from t_A a right outer join t_B b on a.id = b.id;

  

  用(+)来实现, 这个+号可以这样来理解: + 表示补充,即哪个表有加号,这个表就是匹配表。如果加号写在左表,右表就是全部显示,所以是右连接。

Select * from t_A a,t_B b where a.id(+)=b.id;

  

4、全外连接(FULL OUTER JOIN/FULL JOIN)

     左表和右表都不做限制,所有的记录都显示,两表不足的地方均为NULL。 全外连接不支持(+)写法。

select * from t_A a full join t_B b on a.id = b.id;
或
select * from t_A a full outer join t_B b on a.id = b.id;    

  

supplement

select * from t_A a,t_B b where a.id = b.id;
select * from t_A a join t_B b on a.id = b.id;

  

select * from t_A a where a.id in (select b.id from t_B b);
select * from t_A a where exists (select 1 from t_B b where a.id = b.id);

  

Guess you like

Origin www.cnblogs.com/minixiong/p/11230908.html