sql 中startwith 用法
来源:互联网 发布:日语零基础自学 知乎 编辑:程序博客网 时间:2024/06/05 19:44
转:http://myiteyeverywell-163-com.iteye.com/blog/1471752
我们经常会将一个比较复杂的目录树存储到一个表中。或者将一些部门存储到一个表中,而这些部门互相有隶属关系。这个时候你就会用到connect by prior start with。
connect by 是结构化查询中用到的,其基本语法是:
select ... from tablename start with 条件1
connect by prior 条件2
where 条件3;
例:
select * from table
start with org_id = 'HBHqfWGWPy'
connect by prior org_id = parent_id;
简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段:
org_id,parent_id那么通过表示每一条记录的parent是谁,就可以形成一个树状结构。
用上述语法的查询可以取得这棵树的所有记录。
其中:
条件1 是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
条件2 是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR org_id = parent_id就是说上一条记录的org_id 是本条记录的parent_id,即本记录的父亲是上一条记录。
条件3 是过滤条件,用于对返回的所有记录进行过滤。
例如:
没有加中start with ... connect by prior ...的查询结果:
select t.dim_id, t.pid, level
from pmcode.pmcode_fj_tree_rl t
where t.dim_id in (select b.dim_id
from pmcode.PMCODE_KPI_DIM_OD b
where b.kpi_id = 'KC0011')
结果:
DIM_ID PID LEVEL
---------------------
1024 5003 0
1070 0 0
5003 1070 0
5006 0 0
------------------------------------------------------------------------------------
增加start with ... connect by prior ...以后的结果:
select t.dim_id, t.pid, level
from pmcode.pmcode_fj_tree_rl t
where t.dim_id in (select b.dim_id
from pmcode.PMCODE_KPI_DIM_OD b
where b.kpi_id = 'KC0011')
start with t.dim_id = '1070' ----表示从dim_id = '1070'开始(也就是说1070为根节点)
connect by prior t.dim_id = t.pid; ----表示上条记录的dim_id等于本条记录的pid
结果:
DIM_ID PID LEVEL
---------------------
1070 0 1
5003 1070 2
1024 5003 3
- sql 中startwith 用法
- Java中startWith方法的使用!
- js中startWith和endWith的扩展
- Js中StartWith及EndWith的定义
- Java中startWith方法的使用!
- js 中startWith 和 endWith 方法
- Java之split的用法与startWith的用法
- 对js中startWith和endWith的扩展
- 对js中startWith和endWith的扩展
- 对js中startWith和endWith的扩展
- 对js中startWith和endWith的扩展
- SQL中DATALENGTH用法
- SQL中DATALENGTH 用法
- sql中DATEDIFF用法
- SQL中 getdate() 用法
- sql 中case用法
- sql中isnull()用法
- SQL中DATALENGTH 用法
- Context —— getApplicationContext()、getBaseContext()、this的区别
- 欢迎使用CSDN-markdown编辑器
- 如何将pdf文件进行压缩处理
- ORACLE NLS_LENGTH_SEMANTICS 参数问题
- 分布式数据中心数据库和存储部署解决方案
- sql 中startwith 用法
- linux系统安装简介(redhat6.5)
- GitHub上史上最全的iOS开源项目分类汇总
- 数字化企业云平台的Cloud Native12原则
- 牛客网刷题笔记
- VS2010创建并使用动态链接库(.dll)
- Hi3516A开发--安装交叉编译器
- 一点基础知识
- 一些笔试题