update table set col='test' where id in(select id from (select id,rownum num,col1 from table2 order by col1)where mod(num,484,11)=1);
执行此语句之后再执行
update table set col='test2' where id in(select id from (select id,rownum num,col1 from table2 order by col1)where mod(num,484,11)=2);
这样更新的数据就产生了混乱, 第二条sql语句更新的数据有些更新到第一条sql语句当中。希望能给给答复,  到底是数据更新 排序发生了变化而使之rownum也发生了改变。

解决方案 »

  1.   

    不好意思, sql语句有点问题 应该是 where num<=484 and mod(484,11)=1 和 where num<=484 and mod(484,11)=2
      

  2.   

    怎么没人回复!~  顶一下!  顺便改正一下!  table 和table2是同一张表! 
      

  3.   


    select mod(484,11) from dual; ---结果是为0
      

  4.   

    update table set col='test' where id in(select id from (select id,rownum num,col1 from table2 order by col1)where mod(num,11)=1 and num<484);
    原意这样?
      

  5.   

    SQL> create table test
      2  (id number(10),
      3   name varchar2(100));
     
    Table created
     
    SQL> insert into test select rownum,'test'||rownum from dual connect by rownum<=1000;
     
    1001 rows inserted
     
    SQL> commit;
     
    Commit completeSQL> update test a set a.name='test'
      2   where a.id in (select id from (select id ,rownum rn from (select id from test order by id )) where rn<484 and mod(rn,11)=1 );
     
    44 rows updatedSQL> select * from test where name='test' order by id;
     
             ID NAME
    ----------- --------------------------------------------------------------------------------
              1 test
             12 test
             23 test
             34 test
             45 test
             56 test
             67 test
             78 test
             89 test
            100 test
            111 test
            122 test
            133 test
            144 test
            155 test
            166 test
            177 test
            188 test
            199 test
            210 test
     
             ID NAME
    ----------- --------------------------------------------------------------------------------
            221 test
            232 test
            243 test
            254 test
            265 test
            276 test
            287 test
            298 test
            309 test
            320 test
            331 test
            342 test
            353 test
            364 test
            375 test
            386 test
            397 test
            408 test
            419 test
            430 test
            441 test
     
             ID NAME
    ----------- --------------------------------------------------------------------------------
            452 test
            463 test
            474 test
     
    44 rows selected
     
    SQL> 
      

  6.   

    select出数据的时候rownum就注定了,order by没法改变rownum.想要得到安排序的rownum,要多套一层
      

  7.   


    --rownum 与order by层次问题 需先排序 在加序号 也可以分析函数update table set col='test' 
    where id in(
    select id from
    (select id,rownum num from 
    (select id,col1 from table order by col1)
    )  where num <=484 and mod(484,11)=1)
    -- 或者分析函数
    update table set col='test' 
    where id in(
    select id from
    (select id,col1,row_number() over(order by col1) rn
    from table ) where rn<=484 and mod(rn,11)=1)
      

  8.   


    就算我把num 放在外层!  还是一样的! 出来的结果还不是我想要的结果!数据串位了!
      

  9.   


    我现在改成了 row_number() over(order by col1) 同样出现修改的数据串位现象! 
      

  10.   


    这个我测了多次, 前几次都没问题!  多执行几条数据, 而且根据 mod(484,11)=1或者等于2,将每个username值改成不一直。  多试几次就出问题!
      

  11.   

    有这个事情
    update table set col='test' 
    where id in(
    select id from
    (select id,rownum num from 
    (select id,col1 from table order by col1)
    )  where num <=484 and mod(num,11)=1)
      

  12.   

    用rownum+order by将order by放在子查询用row_number()就可以避免一个子查询 因为rownum是按照第一个select的原始记录计算的 不受到order by的影响