当前位置:网站首页>Oracle advanced (IV) table connection explanation
Oracle advanced (IV) table connection explanation
2022-07-06 18:45:00 【InfoQ】
One 、 Preface
- Internal connection ( Natural join )
- External connection (1) The left outer join ( The table on the left is unrestricted )(2) Right connection ( The table on the right is unrestricted )(3) Full outer join ( There are no restrictions on the left and right tables )
- Self join ( Connections in the same table )
select table1.column,table2.column from table1 [inner | left | right | full ] join table2 on table1.column1 = table2.column2;
inner join
Indicates internal connection ;
left join
Left outer connection ;
right join
Right outer connection ;
full join
Indicates full external connection ;
on
Clause is used to specify the connection condition .
- If you use
from
Clause specifies the inside 、 External connection , You have to useon
Clause specifies the connection condition ;
- If you use (
+
) Operator specifies the outer join , Must be usedwhere
Clause specifies the connection condition .
Two 、 Internal connection (Inner Join/Join)
2.1 Inner Join
Inner join
2.2 Illustrate with examples
SQL> select * from dave;
ID NAME
1 dave
2 bl
1 bl
2 dave
SQL> select * from bl;
ID NAME
1 dave
2 bl
SQL> Select a.id,a.name,b.name from dave a inner join bl b on a.id=b.id; -- Standard writing
ID NAME NAME
1 dave dave
2 bl bl
1 bl dave
2 dave bl
SQL> Select a.id,a.name,b.name from dave a join bl b on a.id=b.id; -- Omit here inner keyword
ID NAME NAME
1 dave dave
2 bl bl
1 bl dave
2 dave bl
SQL> Select a.id,a.name,b.name from dave a,bl b where a.id=b.id; -- select Multi-table query
ID NAME NAME
1 dave dave
2 bl bl
1 bl dave
2 dave bl
2.3 Natural join (Natural join)
SQL> Select id,name from dave a natural join bl b;
ID NAME
1 dave
2 bl
SQL> Select dave.id,bl.name From dave join bl on dave.id = bl.id and dave.name=bl.name;
ID NAME
1 dave
2 bl
- If there are multiple fields of two tables with natural connection that have the same name and type , Then they will be regarded as the condition of natural connection .
- If two naturally connected tables only have the same field name , But the data types are different , Then an error will be returned .
3、 ... and 、 External connection (Outer Join)
- (+) Operators can only appear in where clause , And not with outer join Grammar is used at the same time .
- When using (+) When the operator performs an outer join , If in where Clause contains multiple conditions , Must include... In all conditions (+) The operator
- (+) The operator applies only to columns , It can't be used in expressions .
- (+) Operators cannot be associated with or and in Operators are used together .
- (+) Operators can only be used to implement left outer join and right outer join , It cannot be used to realize full external connection .
SQL> select * from bl;
ID NAME
1 dave
2 bl
3 big bird
4 exc
9 Huaining
SQL> select * from dave;
ID NAME
8 Anqing
1 dave
2 bl
1 bl
2 dave
3 dba
4 sf-express
5 dmm
3.1 The left outer join (Left outer join/ left join)
left join
SQL> select * from dave a left join bl b on a.id = b.id;
ID NAME ID NAME
1 bl 1 dave
1 dave 1 dave
2 dave 2 bl
2 bl 2 bl
3 dba 3 big bird
4 sf-express 4 exc
5 dmm -- here B The table is null, Because there is no match to
8 Anqing -- here B The table is null, Because there is no match to
SQL> select * from dave a left outer join bl b on a.id = b.id;
ID NAME ID NAME
1 bl 1 dave
1 dave 1 dave
2 dave 2 bl
2 bl 2 bl
3 dba 3 big bird
4 sf-express 4 exc
5 dmm
8 Anqing
+
+
+
SQL> Select * from dave a,bl b where a.id=b.id(+); -- Be careful : use (+) You have to use keywords where
ID NAME ID NAME
1 bl 1 dave
1 dave 1 dave
2 dave 2 bl
2 bl 2 bl
3 dba 3 big bird
4 sf-express 4 exc
5 dmm
8 Anqing
3.2 Right connection (right outer join/ right join)
left join
SQL> select * from dave a right join bl b on a.id = b.id;
ID NAME ID NAME
1 dave 1 dave
2 bl 2 bl
1 bl 1 dave
2 dave 2 bl
3 dba 3 big bird
4 sf-express 4 exc
9 Huaining -- The left table here is not enough Null fill
-- Have chosen 7 That's ok .
SQL> select * from dave a right outer join bl b on a.id = b.id;
ID NAME ID NAME
1 dave 1 dave
2 bl 2 bl
1 bl 1 dave
2 dave 2 bl
3 dba 3 big bird
4 sf-express 4 exc
9 Huaining -- The left table here is not enough Null fill
-- Have chosen 7 That's ok .
+
+
SQL> Select * from dave a,bl b where a.id(+)=b.id;
ID NAME ID NAME
1 dave 1 dave
2 bl 2 bl
1 bl 1 dave
2 dave 2 bl
3 dba 3 big bird
4 sf-express 4 exc
9 Huaining
3.3 Full outer join (full outer join/ full join)
null
+
SQL> select * from dave a full join bl b on a.id = b.id;
ID NAME ID NAME
8 Anqing
1 dave 1 dave
2 bl 2 bl
1 bl 1 dave
2 dave 2 bl
3 dba 3 big bird
4 sf-express 4 exc
5 dmm
9 Huaining
-- Have chosen 9 That's ok .
SQL> select * from dave a full outer join bl b on a.id = b.id;
ID NAME ID NAME
8 Anqing
1 dave 1 dave
2 bl 2 bl
1 bl 1 dave
2 dave 2 bl
3 dba 3 big bird
4 sf-express 4 exc
5 dmm
9 Huaining
Four 、 Self join
self join
oracle
Select worker.name,Mgr.name From worker,mgr Where worker.id = mgr.id;
select work.ename worker,mgr.ename manager from scott.emp work, scott.emp mgr where work.mgr = mgr.empno order by work.ename;
WORKER MANAGER
ADAMS SCOTT
ALLEN BLAKE
BLAKE KING
CLARK KING
FORD JONES
JAMES BLAKE
JONES KING
MARTIN BLAKE
MILLER CLARK
SCOTT JONES
SMITH FORD
WORKER MANAGER
TURNER BLAKE
WARD BLAKE
5、 ... and 、 Connection diagram

边栏推荐
- [sword finger offer] 60 Points of N dice
- Collection of penetration test information -- use with nmap and other tools
- Crawling data encounters single point login problem
- Test 123
- 2022/02/12
- 图片缩放中心
- Blue Bridge Cup real question: one question with clear code, master three codes
- Markdown syntax for document editing (typera)
- Numerical analysis: least squares and ridge regression (pytoch Implementation)
- STM32+MFRC522完成IC卡号读取、密码修改、数据读写
猜你喜欢
Coco2017 dataset usage (brief introduction)
Blue Bridge Cup real question: one question with clear code, master three codes
巨杉数据库首批入选金融信创解决方案!
Docker installation redis
使用cpolar建立一个商业网站(1)
SQL injection - access injection, access offset injection
44所高校入选!分布式智能计算项目名单公示
Why does wechat use SQLite to save chat records?
Maixll-Dock 摄像头使用
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
随机推荐
10、 Process management
Afnetworking framework_ Upload file or image server
There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
线代笔记....
手写一个的在线聊天系统(原理篇1)
Test 1234
epoll()无论涉及wait队列分析
A method of sequentially loading Unity Resources
30 minutes to understand PCA principal component analysis
Automatic reservation of air tickets in C language
Docker安装Redis
Test 123
Atcoder a mountaineer
复现Thinkphp 2.x 任意代码执行漏洞
bonecp使用数据源
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
巨杉数据库首批入选金融信创解决方案!
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
Blue Bridge Cup real question: one question with clear code, master three codes