如何在 Oracle table 中将逗号分隔值拆分为多行

How to split comma-separated values into multiple rows in Oracle table

SELECT year, movietitle, director, actorname 
  FROM films11 
  WHERE actorname like '%Christina Ricci%' 
  order by year asc;

在 ORACLE SQL Developer 中从原始数据模式生成以下内容。

我想转换整个 table,使主键变成 actor name。 (就像第二个 table)

这样查询

SELECT year, movietitle, director, actorname 
  FROM films11 
  WHERE actorname like '%Christina Ricci%' 
  order by year asc;

将仅生成搜索到的项目(创建新视图或完全更改数据模式。)(第三个 table)

第 1 步:“如何炸毁数据库”

发件人:

SQL Fiddle

Oracle 11g R2 架构设置:

查询 1:

select * from films11

Results:

| YEAR | DIRECTOR | MOVIETITLE |      ACTORNAME |
|------|----------|------------|----------------|
| 2000 |     dir1 |     title1 |      act1,act2 |
| 2001 |     dir2 |     title2 | act1,act2,act3 |
| 2002 |     dir1 |     title3 |           act4 |

查询 2:

select YT.year, YT.movietitle,
       REPLACE(REGEXP_SUBSTR(YT.actorname||',','.*?,',1,lvl.lvl),',','') AS actorname
from films11 YT
join (select level as lvl 
      from dual 
      connect by level <= (select max(regexp_count(actorname,',')+1) from films11)
     ) lvl on lvl.lvl <= regexp_count(YT.actorname,',')+1
     order by YT.year, YT.movietitle, actorname

有很好的笛卡尔积 :

Results:

| YEAR | MOVIETITLE | ACTORNAME |
|------|------------|-----------|
| 2000 |     title1 |      act1 |
| 2000 |     title1 |      act2 |
| 2001 |     title2 |      act1 |
| 2001 |     title2 |      act2 |
| 2001 |     title2 |      act3 |
| 2002 |     title3 |      act4 |

你 运行 它 ONCE 并使用它将所有内容移动到规范化的 DB


这是将您的架构更改为更方便的内容的完整脚本...

CREATE TABLE actors(
    id_actor NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
    act_name VARCHAR2(100)
) 
;

CREATE TABLE directors(
    id_director NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
    dir_name VARCHAR2(100)
) 
;

CREATE TABLE movies(
    id_movie NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
    mov_year NUMBER,
    mov_name VARCHAR2(100),
    director_id NUMBER
) 
;

CREATE TABLE playedby(
    movie_id NUMBER,
    actor_id NUMBER
) 
;

    INSERT INTO directors (dir_name)
    SELECT DISTINCT director dir_name
    FROM films11
    ;

    INSERT INTO movies (mov_year, mov_name, director_id)
    SELECT year mov_year, movietitle mov_name, directors.id_director director_id
    FROM films11
    INNER JOIN directors ON directors.dir_name = films11.director

    ;

    INSERT INTO actors (act_name)
    SELECT DISTINCT t.actorname act_name
    FROM (
        SELECT YT.year, YT.movietitle,
               REPLACE(REGEXP_SUBSTR(YT.actorname||',','.*?,',1,lvl.lvl),',','') AS actorname
        FROM films11 YT
        JOIN (SELECT level AS lvl 
              FROM dual 
              CONNECT BY level <= (SELECT MAX(REGEXP_COUNT(actorname,',')+1) FROM films11)
             ) lvl ON lvl.lvl <= REGEXP_COUNT(YT.actorname,',')+1
    ) t
    ;

    INSERT INTO playedby (movie_id, actor_id)
    SELECT movies.id_movie movie_id, actors.id_actor actor_id
    FROM (
        SELECT YT.year, YT.movietitle,
               REPLACE(REGEXP_SUBSTR(YT.actorname||',','.*?,',1,lvl.lvl),',','') AS actorname
        FROM films11 YT
        JOIN (SELECT level AS lvl 
              FROM dual 
              CONNECT BY level <= (SELECT MAX(REGEXP_COUNT(actorname,',')+1) FROM films11)
             ) lvl ON lvl.lvl <= REGEXP_COUNT(YT.actorname,',')+1
    ) t
    INNER JOIN actors ON t.actorname = actors.act_name
    INNER JOIN movies ON t.year = movies.mov_year AND t.movietitle = movies.mov_name 

    ;

之后你可以像这样制作一个 select :

查询 3:

SELECT mov_year, mov_name, dir_name, act_name 
FROM movies
INNER JOIN directors ON directors.id_director = movies.director_id
INNER JOIN playedby ON movies.id_movie = playedby.movie_id
INNER JOIN actors ON playedby.actor_id = actors.id_actor
WHERE act_name like '%act2%' 
order by mov_year asc

Results:

| MOV_YEAR | MOV_NAME | DIR_NAME | ACT_NAME |
|----------|----------|----------|----------|
|     2000 |   title1 |     dir1 |     act2 |
|     2001 |   title2 |     dir2 |     act2 |