惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
博客园 - 聂微东
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
L
LangChain Blog
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
S
Secure Thoughts
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Help Net Security
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
G
Google Developers Blog
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
Kaspersky official blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives

博客园 - 电电儿

FME Cloud 账号申请流程 FME中通过HTMLExtractor向HTML要数据 在windows 2003下安装 django + apache + mod_python arcgis中坐标系统的简单描述 arcgis 中利用txt坐标文件创建要素的办法,含txt文件详细格式~ Django静态文件配置备忘录 【转】ARCSDE+ORACLE备份 测量坐标系中单个多边形面积解析法计算的程序源代码 - 电电儿 - 博客园 测试oracle with as System.Data.SQLite测试 oracle左右连接的另外表示方法 Oracle 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数,其他函数) Oracle中的Union、Union All、Intersect、Minus inner join,full outer join,left join,right join,cross join Oracle 中的natural join (自然连接) ORACLE JOIN 用正则表达式分析正则表达式!求正则表达式组数~ - 电电儿 - 博客园 BoooLee pyretoolkit -- 一个基于python re模块的在线正则表达式测试工具 GDAL 在windows python环境下的安装步骤
Oracle中的自连接(self join)
电电儿 · 2009-09-13 · via 博客园 - 电电儿

自连接(self join)是SQL语句中经常要用的连接方式,使用自连接可以将自身表的一个镜像当作另一个表来对待,从而能够得到一些特殊的数据。

我们什么时候应该使用自连接呢?我们来看下面的例子。

在oracle的scott的schema中有一个表是emp

在emp中的每一个员工都有自己的mgr(经理),并且每一个经理自身也是公司的员工,自身也有自己的经理。下面我们需要将每一个员工自己的名字和经理的名字都找出来。这时候我们该怎么做呢?

如果我们有两张这样的表分别教worker和mgr,那么我们就很好写SQL语句。

Select worker.name,

Mgr.name

From worker,mgr

Where worker.id = mgr.id;

但现在我们只有一张表。你也许说我们现在在建一张表,把同样的数据拷贝过去不就可以了吗?是的,这样可以,但我们不会采用,因为这样就会很麻烦,而且数据严重冗余等等很多弊端。

我们有更好的方法,那就是自连接。

自连接的本意就是将一张表看成多张表来做连接。我们可以这样来写SQL语句

select work.ename “工人”,’ works for’,mgr.ename “老板”

from emp work, emp mgr

where work.mgr = mgr.empno

order by work.ename;

得到了如下结果:

工人 ‘WORKSFOR’ 老板

——————– ——————– ——————–

ADAMS works for SCOTT

ALLEN works for BLAKE

BLAKE works for KING

CLARK works for KING

FORD works for JONES

JAMES works for BLAKE

JONES works for KING

MARTIN works for BLAKE

MILLER works for CLARK

SCOTT works for JONES

SMITH works for FORD

TURNER works for BLAKE

WARD works for BLAKE

这里我们注意到,King这个人没有出现在工人列里面,是因为这个人是整个公司的老板,他没有经理。如果这时候我们希望king这个人也出现在左侧,即使他没有老板。

我们可以使用刚刚才介绍过的oracle中外连接

例如:

select work.ename “工人”,’ works for’,mgr.ename “老板”

from emp work, emp mgr

where work.mgr = mgr.empno (+)

order by work.ename;

得到结果:

工人 ‘WORKSFOR’ 老板

——————– ——————– ———-

ADAMS works for SCOTT

ALLEN works for BLAKE

BLAKE works for KING

CLARK works for KING

FORD works for JONES

JAMES works for BLAKE

JONES works for KING

KING works for

MARTIN works for BLAKE

MILLER works for CLARK

SCOTT works for JONES

SMITH works for FORD

TURNER works for BLAKE

WARD works for BLAKE

这是我们看到king的右侧是空白,也就是没有老板。

从这里我们看到,当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自连接。