系统中有一个表如下
id   layer
01   2
02   2
03   3我想实现一个这样的结果,就是当查询该表时layer字段是几就会查出几条记录,谢谢

解决方案 »

  1.   

    你对结果集有什么要求吗?比如,layer=2时,你要返回表里哪2条记录?还是随机返回记录即可?
      

  2.   

    结果是对同一条记录如果LAYER是2 id是01 那么产生结果是
    011
    012 这样的
      

  3.   

    --随机取(参数id可变)
    with test as(
      select '01' id, 2 layer from dual union all
      select '02' id, 2 layer from dual union all
      select '03' id, 3 layer from dual)
    SELECT * FROM test WHERE rownum < (SELECT layer FROM test WHERE id = '01') + 1;--按升序取(ID参数可变)
    with test as(
      select '01' id, 2 layer from dual union all
      select '02' id, 2 layer from dual union all
      select '03' id, 3 layer from dual)
    SELECT *
      FROM (SELECT * FROM test ORDER BY id)
     WHERE rownum < (SELECT layer FROM test WHERE id = '01') + 1;
      

  4.   

    看看这个是不是你要的 with id_layer as(
      select '01' id, 2 layer from dual union all
      select '02' id, 2 layer from dual union all
      select '03' id, 3 layer from dual)
    select t1.id||t3.rn   
      from id_layer t1,
           (select rownum rn from dual connect by rownum<=(select max(t2.layer) from id_layer t2))t3
     where t3.rn<=t1.layer 
      

  5.   


    select id,layer from (select id,layer,rownum rn from table) a
    where a.rn<=(select layer from table where id='01')
      

  6.   

    谢谢大家有些事情我没说清针对不去 记录中的layer不是固定的值 会是4,5,8 或争夺
    返回结果中
    是 对应记录的ID + 数值 这个数值时1到layer值之间的值
      

  7.   

    那就是 with id_layer as(
      select '01' id, 2 layer from dual union all
      select '02' id, 2 layer from dual union all
      select '03' id, 3 layer from dual)
    select t1.id||t3.rn   
      from id_layer t1,
           (select rownum rn from dual connect by rownum<=(select max(t2.layer) from id_layer t2))t3
     where t3.rn<=t1.layer 你试的时候 
    with id_layer as(
      select '01' id, 2 layer from dual union all
      select '02' id, 2 layer from dual union all
      select '03' id, 3 layer from dual) 
    这段不要,然后把id_layer换成你的表名就可以了