col1(id)      col2       col3
a               1          3
b               2          5
能不能用一句SQL来实现下面结果
a       1
a       2
a       3
b       2
b       3
b       4
b       5
谢谢啦

解决方案 »

  1.   

    很简单的层次查询
    with t as
     (select 'a' col1, 1 col2, 3 col3
        from dual
      union all
      select 'b', 2, 5
        from dual
      union all
      select 'c', 9, 33 from dual)
    select distinct col1, col2 + level - 1
      from t
    connect by level <= col3 - col2 + 1
     order by col1;
      

  2.   

    我擦,我怎么看不懂,connect by?
      

  3.   


    看不懂?不是搞oracle开发的?
      

  4.   


    看不懂?不是搞oracle开发的?没学过这种查询。。刚查了下不太明白
      

  5.   


    declare
    cursor cur_a is select * from A;
    begin
    for a_row in cur_a loop
    for i in a_row.col2..a_row.col3 loop
    dbms_output.put_line(a_row.col1||' '||i);
    end loop;
    end loop;
    end;用游标cur_a获取表A中的数据,二层for循环,外层遍历游标,内层遍历该行数据
      

  6.   

    a何来对应2?b何来对应3、4? 不明白lz意思又或是lz写错了?
      

  7.   

    u010412956,你的方法是正确的,但是现在发现一个问题,当原表(t)的记录数比如col1= ‘a'在几十条以上,这个语句的查询效率实在是太差了,需要几十秒钟才能完成!!!有没有效率更高一些的语句,谢谢!!!