• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Oracle中的树查询与CASE查询

mysql 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

当表具有层次结构数据时,通过使用层次查询可以更直观的显示数据结果,并显示其数据之间的层次关系。如下查询例子:

当表具有层次结构数据时,通过使用层次查询可以更直观的显示数据结果,并显示其数据之间的层次关系。如下查询例子:

select lpad(‘ ‘,3*(t2.grl_grouplevel-1))||t1.cif_namecn from cb_cst_inf t1,cs_group_rel t2 where t1.cif_cstno=t2.grl_cstid
start with t2.grl_cstid =’CB10001810′ and t2.grl_groupid=’10002135′
connect by prior t2.grl_cstid = t2.grl_uplevelid;
–cs_group_rel表中grl_uplevelid字段体现层次关系,,最上级grl_uplevelid为空
–START WITH :用于指定层次查询的跟行
–CONNECT BY :用于指定父行和子行之间的关系,在这个条件中必须使用PRIOR引用父行,语法如下:…PRIOR expr=expr 或 …expr=PRIOR expr

CASE查询:
select lpad(‘ ‘,5*(t2.grl_grouplevel-1))||t1.cif_namecn name,
case when t2.grl_grouplevel=’1′ then ‘总公司’
when t2.grl_grouplevel=’2′ then ‘一级分公司’
else ‘一级以下分公司’ end level_name from cb_cst_inf t1,cs_group_rel t2 where t1.cif_cstno=t2.g

本文来源gaodai^.ma#com搞#代!码网

rl_cstid –and t1.cif_cstno=’CB10001810′
start with t2.grl_uplevelid is null and t2.grl_groupid=’10002024′
connect by prior t2.grl_cstid = t2.grl_uplevelid;


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Oracle中的树查询与CASE查询
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址