日期:2014-05-17 浏览次数:20696 次
/**创建测试的表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的一个列,作为“辅助列”**/ [color=#FF0000]SELECT LEVEL l FROM dual CONNECT BY LEVEL<=10;[/color] /**最终的SQL**/ SELECT T1.ID, 'A1-'||T3.DZ AS LIST FROM T1, T2, (SELECT LEVEL DZ FROM DUAL CONNECT BY LEVEL <= 10) T3 WHERE T1.PID = T2.PID AND T3.DZ <= T2.PNUMBER ORDER BY 1,2;