我有一个表code | name
0101    XX
0102    XX
0201    XX
0102    XX
0301    XX
0302    XX
0401    XX数据类似这样的,我想写一条语句查询出CODE字段不是以01或02开头的记录也就是把0301,0302,0401查询出来.不知道有没有合适的方法谢谢大家

解决方案 »

  1.   

    我只想到了用子查询里写like语句的方法比如:select * from A where code not in 
    (
     select code from A a where a.code like '01%' or a.code like '02%'
    )但是我担心这样写效率低因为子查询里的查询条件是我根据用户在前台的选择结果拼出来的这个表里的记录最多有70万条,一般的有1,2万条,小的也有几千条如果这样做的话,查询70万条记录的那个表的时候是不是会把应用服务器干死啊?所以来这里找高手来指点我一下
      

  2.   

    改成这样
    select * from A a where a.code not like '01%' AND  a.code NOT like '02%'
    或者还可以这样
    select * from A where substr(code,1,2) not in  ('01','02')
      

  3.   


    select * from A where substr(code,1,2) not in('01','02') 
      

  4.   

    select * from your_tab
    where 
    regexp_like(code,'^[^01|02]');
      

  5.   

    我认为这样效率高点
    select * from a
    where code<'01' or code>'02'
      

  6.   


    回5楼的朋友,我倒不是想一下全显示出来,我的意思是,我用like做过滤,不管怎么分页,都是做全表扫描啊。而且我听说用like效率很低况且在程序执行的时候有很多like or like会影响效率。这么多朋友给了回复,感谢大家。我看大家给了2,3种方法,不知道大家认为哪种方法在效率上比较有优势呢?或者说从分析Oracle执行过程的角度分析分析谢谢。请高手们继续
      

  7.   


    drop table tb;
    create table tb(code varchar2(4), name varchar2(2));
    insert into tb
    select '0101','XX' from dual union all
    select '0102','XX' from dual union all
    select '0201','XX' from dual union all
    select '0102','XX' from dual union all
    select '0301','XX' from dual union all
    select '0302','XX' from dual union all
    select '0401','XX' from dual;--查询结果
    select * from tb where code not like '01%' and code not like '02%';--结果
    /*
    0301 XX
    0302 XX
    0401 XX
    */
      

  8.   


    select * from tb
    where code<'01' or code>'02zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
      

  9.   

    你的数据是01,02,03,04这样的,可以写成>=03就可以了select * from t where code >= '03';
      

  10.   

    select * from a where code.substring(1,1) not in ('1','2');
      

  11.   

    从性能的角度来看,
    我觉得下面这种效率高一下;
    select * from tb where code not like '01%' and code not like '02%';
    其他的基本上没法使用索引;
      

  12.   

    select * from table where substr(code,1,2) not in('01','02')
      

  13.   

    select * from A where instr(code,'01') <> 1 and instr(code,'02') <> 1 普遍认为instr有很好的表现,我基本用它来代替like了,并且lz还不能用索引,这个东西的优势更明显了
      

  14.   

    select * from tb
    where code<'01' or code>='03'like和in的性能并不好
      

  15.   

    顶一下
       oracle QQ群:54775466
               欢迎爱好者 一起入群探讨
      

  16.   

    select * from t where code >= '03';
      

  17.   

    select * from your_tab
    where
    regexp_like(code,'^[^01|02]');
    上面这个是什么函数啊
      

  18.   

    code | name
    0101 XX
    0102 XX
    0201 XX
    0102 XX
    0301 XX
    0302 XX
    0401 XX
    select t.code,t.name
     from table_name t 
    where substr(t.code,1,2) not in ('01','02')
      

  19.   


     
    SQL> 
    SQL> create table tb(code varchar2(4), name varchar2(2));
     
    Table created
    SQL> insert into tb
      2  select '0101','XX' from dual union all
      3  select '0102','XX' from dual union all
      4  select '0201','XX' from dual union all
      5  select '0102','XX' from dual union all
      6  select '0301','XX' from dual union all
      7  select '0302','XX' from dual union all
      8  select '0401','XX' from dual;
     
    7 rows inserted
     
    SQL> select * from tb where code not like '01%' and code not like '02%';
     
    CODE NAME
    ---- ----
    0301 XX
    0302 XX
    0401 XX
     
    SQL> 
      

  20.   

    第一想到的就是  not like '01%' and not like '02%'