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

推荐订阅源

Y
Y Combinator Blog
腾讯CDC
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
博客园_首页
D
DataBreaches.Net
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
月光博客
月光博客
Jina AI
Jina AI
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 【当耐特】
Vercel News
Vercel News
WordPress大学
WordPress大学
J
Java Code Geeks
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42

博客园 - mp3 swf

好书推荐:全世界人民都知道 下载 128码和39码编码规则 Five particular problem areas in marriage Be still 片刻停留,片刻自我 热播剧《我的兄弟叫顺溜》同名小说(全书)TXT下载 哈佛图书馆墙上的训言 C# 4.0 “修复了”死锁问题 我的团长我的团 原著小说 文字版 best way to get something done at work ORACLE 数据库调优 策略参考 oracle 存储过程的基本语法 学,无止境 Oracle 10g OCP认证资料(官方教材与考试题库)下载 美文赏析:天使降落在人间 ASP.NET 2.0实现自带TreeView的客户端连带选择 终于把ORACLE 10g的2门考试给搞结束了 功劳是老板的、钱财是子女的、身体是自己的... Oracle数据表管理建议 Oracle 10g 实例恢复Tuning
Oracle中针对层次数据所设计的专用SQL查询语句
mp3 swf · 2009-01-22 · via 博客园 - mp3 swf

假设有组织数据表orgstdstruct,其中包含UNITID,PUNITID,UNITNAME等字段,其中PUNITID是本表UNITID字段的外键,那么在ORACLE中可以通过以下语句直接查询出具有层次关系的数据,使用起来非常方便。

select level,unitid,punitid,unitname from orgstdstruct
start with punitid is null
connect by prior unitid = punitid

语法结构如下:

SELECT [LEVEL],column,expr...

FROM table

[WHERE condition(s)]

[START WITH condition(s)]

[CONNECT BY PRIOR condition(s)];

其中LEVEL字段是可选的,表示查询出来的数据层级

在Oracle 10g中,增加了一个新函数,叫做CONNECT_BY_ISLEAF,如果行的值为0表示非叶子节点,1表示叶子节点,示例如下:

select level,unitid,punitid,unitname,

(case when connect_by_isleaf=1 then '叶子' else '不是叶子' end) isleaf

from orgstdstruct
start with punitid is null
connect by prior unitid = punitid

另外,Oracle 10g还提供了CONNECT_BY_ISCYCLE和NOCYCLE关键字来解决循环问题,示例如下:

select level,connect_by_iscycle,unitid,punitid,unitname from orgstdstruct
start with punitid is null
connect by nocycly prior unitid = punitid

这样可以避免循环参加查询操作,并且通过CONNECT_BY_ISCYCLY得到哪个节点发生循环,0表示未循环,1表示循环

遗憾的是这种语法目前SQLServer还未支持,也不是标准的T-SQL语法。