Oracle SQL连接在具有多个换行符(chr(10))分隔值的列上

时间:2016-05-06 15:26:03

标签: sql oracle join

以下是我必须加入的示例表。

SQL> select 'CH1' chapter , 'HELLO'||chr(10)||'WORLD' output from dual union
  2  select 'CH2' chapter , 'HELLO'||chr(10)||'GALAXY' output from dual union
  3  select 'CH3' chapter , 'HELLO'||chr(10)||'UNIVERSE'  output from dual;

CHAPTER         OUTPUT
--------------- --------------
CH1             HELLO
                WORLD

CH2             HELLO
                GALAXY

CH3             HELLO
                UNIVERSE

SQL> select 'WORLD' output, 'PG1' Page from dual union
  2  select 'GALAXY' output, 'PG2' Page from dual union
  3  select 'UNIVERSE' output, 'PG3' Page from dual;

    OUTPUT   PAGE
-------- ------------
GALAXY   PG2
UNIVERSE PG3
WORLD    PG1

第一个表中的OUTPUT列有多个值与chr(10)分开,我想与第二个表的OUTPUT列连接,以便输出如下所示:

CHAPTER         OUTPUT          PAGE
--------------- --------------  ----------------
CH1             HELLO               P1
                WORLD

CH2             HELLO               P2
                GALAXY

CH3             HELLO               P3
                UNIVERSE

提前致谢。

4 个答案:

答案 0 :(得分:1)

select chapter, c.output, page
from   table_chapters c join table_pages p
   on c.output like '%' || p.output || '%'
order by chapter, page

如果“pages”表中的输出是“chapters”表中输出的精确子字符串,则连接条件匹配。我认为这就是你需要的。

如果您需要按照我的显示排序输出,必须小心,因为在词典排序中P10在P3之前。如果页码是NUMBER格式,而不是字符串格式,则为最佳。与章节相同。

答案 1 :(得分:0)

如果需要始终在字符串的第二行进行连接,则可以使用:

SQL> select chapter, a.output, page
  2  from test_a A
  3       inner join test_B B
  4         on ( substr(a.output, instr(a.output, chr(10))+1, length(a.output)) = B.output)
  5  order by chapter;

CHA OUTPUT         PAG
--- -------------- ---
CH1 HELLO          PG1
    WORLD

CH2 HELLO          PG2
    GALAXY

CH3 HELLO          PG3
    UNIVERSE

答案 2 :(得分:0)

select A.*, B.PAGE from
(select 'CH1' chapter , 'HELLO'||chr(10)||'WORLD' output from dual union
select 'CH2' chapter , 'HELLO'||chr(10)||'GALAXY' output from dual union
select 'CH3' chapter , 'HELLO'||chr(10)||'UNIVERSE'  output from dual ) A
JOIN
(select 'WORLD' output, 'PG1' Page from dual union
select 'GALAXY' output, 'PG2' Page from dual union
select 'UNIVERSE' output, 'PG3' Page from dual) B
on a.output like '%'||b.output||'%';

CHAPTER         OUTPUT          PAGE
--------------- --------------  ----------------
CH1             HELLO               P1
                WORLD

CH2             HELLO               P2
                GALAXY

CH3             HELLO               P3
                UNIVERSE

答案 3 :(得分:0)

试试这个

SELECT 
    A.chapter, 
    A.output, 
    B.Page
FROM
    (select 'CH1' chapter , 'HELLO'||chr(10)||'WORLD' output from dual union
     select 'CH2' chapter , 'HELLO'||chr(10)||'GALAXY' output from dual union
     select 'CH3' chapter , 'HELLO'||chr(10)||'UNIVERSE'  output from dual) A,
    (select 'WORLD' output, 'PG1' Page from dual union
     select 'GALAXY' output, 'PG2' Page from dual union
     select 'UNIVERSE' output, 'PG3' Page from dual) B
WHERE
    A.output LIKE '%'||B.output
ORDER BY A.chapter;

示例输出

SQL> SELECT
  2     A.chapter,
  3     A.output,
  4     B.Page
  5  FROM
  6     (select 'CH1' chapter , 'HELLO'||chr(10)||'WORLD' output from dual union
  7      select 'CH2' chapter , 'HELLO'||chr(10)||'GALAXY' output from dual union
  8      select 'CH3' chapter , 'HELLO'||chr(10)||'UNIVERSE'  output from dual) A,
  9     (select 'WORLD' output, 'PG1' Page from dual union
 10      select 'GALAXY' output, 'PG2' Page from dual union
 11      select 'UNIVERSE' output, 'PG3' Page from dual) B
 12  WHERE
 13     A.output LIKE '%'||B.output
 14  ORDER BY A.chapter;

CHA OUTPUT         PAG
--- -------------- ---
CH1 HELLO          PG1
    WORLD

CH2 HELLO          PG2
    GALAXY

CH3 HELLO          PG3
    UNIVERSE