Home >Database >Mysql Tutorial >Oracle 一行转多行

Oracle 一行转多行

WBOY
WBOYOriginal
2016-06-07 17:15:381069browse

/**解决方法的核心是:产生出1-10的一个列,作为“辅助列”**/ SELECT LEVEL l FROM dual CONNECT BY LEVELlt;=10; 【问题】

/**创建测试的表T1 也就是楼主说的A表**/

CREATE TABLE t1(

  ID VARCHAR2(10),

  pid VARCHAR2(10)

);

/**创建测试的表T2 也就是楼主说的B表**/

CREATE TABLE t2(

  pid VARCHAR2(10),

  pnumber VARCHAR2(10)

);

/**插入T1的数据,插入T2的测试数据**/

INSERT INTO t1 VALUES('A1','1');

INSERT INTO t1 VALUES('A2','2');

INSERT INTO t1 VALUES('A3','3');

INSERT INTO t2 VALUES('1','2');

INSERT INTO t2 VALUES('2','3');

INSERT INTO t2 VALUES('3','5');


/**解决方法的核心是:产生出1-10的一个列,作为“辅助列”**/

SELECT LEVEL l FROM dual CONNECT BY LEVEL


/**最终的SQL**/

SELECT T1.ID, 'A1-'||T3.DZ AS LIST

  FROM T1, T2, (SELECT LEVEL DZ FROM DUAL CONNECT BY LEVEL

 WHERE T1.PID = T2.PID

   AND T3.DZ

 ORDER BY 1,2;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn