码迷,mamicode.com
首页 > 数据库 > 详细

oracle with 语句实现递归查询

时间:2018-06-26 12:19:59      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:col   detail   vpd   ring   term   oracle   break   any   soft   

Oracle with 语句可以实现如同connect by 语句一样的序列:


connect by用法

使用rownum实现1到10的序列。

select rownum from dual connect by rownum<=10;

技术分享图片

使用level实现1到10的序列。

select level from dual connect by level<=10;

技术分享图片


with 可实现同样功能用法:

with c(n) as
(select 1 from dual
union all
select n+1 from c
where n<10)
select n from c;

技术分享图片


更多connect by 用法参考:https://blog.csdn.net/wang_yunj/article/details/51040029/


查询当前时间往前的12周的开始时间、结束时间、第多少周:

select sysdate - (to_number(to_char(sysdate - 1, 'd')) - 1) -       (rownum - 1) * 7 as startDate,       sysdate + (7 - to_number(to_char(sysdate - 1, 'd'))) -       (rownum - 1) * 7 as endDate,       to_number(to_char(sysdate, 'iw')) - rownum + 1 as weekIndex  from dualconnect by level<= 12;--将level改成rownum可以实现同样的效果

技术分享图片

  • d 表示一星期中的第几天

  • iw 表示一年中的第几周


字符串分割,由一行变为多行:

  1. 比如说分割01|02|03|04这种有规律的字符串

select REGEXP_SUBSTR('01|02|03|04', '[^|]+', 1, rownum) as newport 
 from dual 
connect by rownum <= REGEXP_COUNT('01|02|03|04', '[^|]+');


oracle with 语句实现递归查询

标签:col   detail   vpd   ring   term   oracle   break   any   soft   

原文地址:http://blog.51cto.com/aaron521/2132730

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!