select * from (select * from 表a where sex='男' order by age) where rownum<=10
 上面这条语句是可以查找出来的,但是语句执行时间有点长,比select * from 表a where sex='男' and rownum<=10慢多了
但如果加上order,那条语句就会报错,有没有高手能给出既能排序执行速度又快的sql语句啊!!!
  其实我是想修改前十数据的值,想用的方法是
  update 表a set note='aaa' where id in(select id from (select id from 表a where sex='男' order by age) where rownum<=10)这条语句,这个方法对吗?有好点的sql语句吗???

解决方案 »

  1.   

    select * from 表 where sex = '男' and rownum <=10 order by age
    没有发现错误
      

  2.   

    select * from (select * from 表 where sex = '男' order by age)
    where rownum <=10 
      

  3.   


    select * from 表a where sex='男' and rownum<=10慢多了
    --这个肯定是有问题了 你的直接是给每个记录加个序号update 表a set note='aaa' where id in(select id from (select id from 表a where sex='男' order by age) where rownum<=10)
    --这个是可以的--也可以用下面的
    update 表a set note='aaa' where id in
    (select id from (select id,row_number() over(order by age,rowid) rn from 表a where sex='男') t where rn<=10)
      

  4.   

    对AGE加索引不行么?加完索引之后再SELECT,我是初学者,不知道这样行不~
      

  5.   

    上来发现被人写了,噢。
    UPDATE table1 a SET note='aaa' 
     WHERE exists (SELECT 1 
                     FROM (SELECT id,age,row_number() over(order by age,id) rk FROM table1 
                            WHERE sex='男') b 
                    WHERE rk<=10 and a.id=b.id);
                           
    create table table1 (
      id   VARCHAR2(2),
      sex  VARCHAR2(2),
      age  number
      );
    alter table table1 add  note VARCHAR2(3) default 'bbb';  --增加一列
    SELECT * FROM table1 ; 
    INSERT INTO table1 VALUES('01','男',10);  
    INSERT INTO table1 VALUES('02','男',14); 
    INSERT INTO table1 VALUES('03','男',11); 
    INSERT INTO table1 VALUES('04','男',14); 
    INSERT INTO table1 VALUES('05','男',10); 
    INSERT INTO table1 VALUES('06','男',13); 
    INSERT INTO table1 VALUES('07','女',25); 
    INSERT INTO table1 VALUES('08','男',23); 
    INSERT INTO table1 VALUES('09','男',36);
    INSERT INTO table1 VALUES('10','男',35); 
    INSERT INTO table1 VALUES('11','男',24); 
    INSERT INTO table1 VALUES('12','男',25); 
    INSERT INTO table1 VALUES('13','男',24); 
    INSERT INTO table1 VALUES('14','男',15); 
    INSERT INTO table1 VALUES('15','男',13); 
      

  6.   

    exists 肯定要比 in快很多
      

  7.   

    我在pl/sql中运行你和4楼写的语句,都直接死在那不动了,只能强制关才能关!!怎么回事啊?
    我的sql语句是:update yuqingdongtai a set isextract='tjf' where exists (select id from (select id ,row_number() over (order by generatetime,id)rn from yuqingdongtai where ishulue=0 and isignore=0 and isextract='0' )b where rn <=20 and a.id=b.id )
      

  8.   

    语句可以这么写update
    (
    select * from (select * from 表a where sex='男' order by age) where rownum<=10
    )tmp
    set tmp.note='aaa' ;至于exists和in谁快来说,要看表的数据条数,这里我想应该是in更快,
    in需要做的事
    1.找出男性同胞,
    2.然后在临时表空间排序,
    3.取出前十条
    4.排序10条数据,和父查询匹配
    exists需要做的事
    1.找出男性同胞,
    2.然后在临时表空间排序,
    3.取出前十条
    4.不排序但是表a中的每条数据都要和子查询中id匹配,如果有1千万条就要匹配1千万次,效率远低于排序10条数据....
    所以exists不是总比in快
      

  9.   

    补充
    in子查询只执行了1次
    exists子查询执行了select count(1) from a;次