要获取下面的输出(Oracle 11g),select查询将是什么?

时间:2019-02-12 07:19:44

标签: sql oracle

Oracle 11g中的选择查询是什么?

Input Table
------------------
Feed_id    User_Id
CL300      K111222;K555444;M123123

Required Output
--------------------------
Feed_id    User_Id
CL300       K111222
CL300       K555444
CL300       M123123

2 个答案:

答案 0 :(得分:1)

以这种方式尝试:

SQL Fiddle

Oracle 11g R2架构设置

var input = document.getElementById('inputUser').value;
var length = StringLength(input);

查询1

CREATE TABLE t
    ("Feed_id" varchar2(5), "User_Id" varchar2(23))
;

INSERT ALL 
    INTO t ("Feed_id", "User_Id")
         VALUES ('CL300', 'K111222;K555444;M123123')
SELECT * FROM dual
;

Results

select
  "Feed_id",
  trim(regexp_substr("User_Id", '[^;]+', 1, levels.column_value))  as User_Id
from 
  t,
  table(cast(multiset(select level from dual connect by  level <= length (regexp_replace("User_Id", '[^;]+'))  + 1) as sys.OdciNumberList)) levels

答案 1 :(得分:0)

 WITH input_table AS 
 (
   SELECT  'CL300'                   AS  feed_id, 
           'K111222;K555444;M123123' AS user_id 
   FROM     dual
 )
 SELECT   feed_id,
          REGEXP_SUBSTR(REPLACE(REPLACE(user_id, ';;', '; ;'),';;','; ;'), '[^;]+', 1, LEVEL) AS user_id
 FROM     input_table
 CONNECT  BY
          REGEXP_SUBSTR(REPLACE(REPLACE(user_id, ';;', '; ;'),';;','; ;'), '[^;]+', 1, LEVEL) IS NOT NULL