zoukankan      html  css  js  c++  java
  • Oracle SQL 劈开字符串

    一、数据样例

    二、劈开单行

    SELECT id_,REGEXP_SUBSTR(name_, '[^,]+', 1, rownum) h2
      FROM  (select id_,name_ from test_reg_count t
      where t.id_ =2 )
    CONNECT BY level <= length(name_)-length(replace(name_,',',''))+1;
    
    --或者
    SELECT id_,REGEXP_SUBSTR(name_, '[^,]+', 1, rownum) h2
      FROM  (select id_,name_ from test_reg_count t
      where t.id_ =2 )
    CONNECT BY REGEXP_SUBSTR(name_, '[^,]+', 1, rownum) is not null;

    三、劈开多行

    SELECT id_,REGEXP_SUBSTR(name_, '[^,]+', 1, level) name_
      FROM  test_reg_count   
    CONNECT BY level <= length(name_)-length(replace(name_,',',''))+1         
           AND name_ = PRIOR name_          
           AND PRIOR dbms_random.value <1;
           
    --或者
    SELECT id_,REGEXP_SUBSTR(name_, '[^,]+', 1, level) name_
      FROM  test_reg_count   
    CONNECT BY level <= length(name_)-length(replace(name_,',',''))+1         
           AND name_ = PRIOR name_          
           AND PRIOR SYS_GUID() is not null;

    这里最重要的是 prior操作符,关于prior的解释Oracle的官方文档是这么描述的:

      In a hierarchical query, one expression in the CONNECT BY condition must be qualified by the PRIOR operator. If the CONNECT BY condition is compound, then only one condition requires the PRIOR operator, although you can have multiple PRIOR conditions. PRIOR evaluates the immediately following expression for the parent row of the current row in a hierarchical query.

      PRIOR is most commonly used when comparing column values with the equality operator. (The PRIOR keyword can be on either side of the operator.) PRIOR causes Oracle to use the value of the parent row in the column. Operators other than the equal sign (=) are theoretically possible in CONNECT BY clauses. However, the conditions created by these other operators can result in an infinite loop through the possible combinations. In this case Oracle detects the loop at run time and returns an error.

  • 相关阅读:
    解读基本数据类型和内置方法(1)(要掌握)
    简单循环流程的介绍
    基本数据类型的使用和运算符的介绍
    开辟编程语言的介绍和变量
    HTML5 元素超出部分滚动, 并隐藏滚动条
    数据库多行数据合并一行(sqlserver、Oracle、Mysql)
    Js apply方法与call方法详解 附ES6新写法
    Java实现牛顿迭代法求解平方根、立方根
    为什么在JavaScript中0.1+0.2不等于0.3?
    html5手机web页面底部菜单
  • 原文地址:https://www.cnblogs.com/Alex-Zeng/p/5920691.html
Copyright © 2011-2022 走看看