我有一个表code | name
0101 XX
0102 XX
0201 XX
0102 XX
0301 XX
0302 XX
0401 XX数据类似这样的,我想写一条语句查询出CODE字段不是以01或02开头的记录也就是把0301,0302,0401查询出来.不知道有没有合适的方法谢谢大家
0101 XX
0102 XX
0201 XX
0102 XX
0301 XX
0302 XX
0401 XX数据类似这样的,我想写一条语句查询出CODE字段不是以01或02开头的记录也就是把0301,0302,0401查询出来.不知道有没有合适的方法谢谢大家
(
select code from A a where a.code like '01%' or a.code like '02%'
)但是我担心这样写效率低因为子查询里的查询条件是我根据用户在前台的选择结果拼出来的这个表里的记录最多有70万条,一般的有1,2万条,小的也有几千条如果这样做的话,查询70万条记录的那个表的时候是不是会把应用服务器干死啊?所以来这里找高手来指点我一下
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')
select * from A where substr(code,1,2) not in('01','02')
where
regexp_like(code,'^[^01|02]');
select * from a
where code<'01' or code>'02'
回5楼的朋友,我倒不是想一下全显示出来,我的意思是,我用like做过滤,不管怎么分页,都是做全表扫描啊。而且我听说用like效率很低况且在程序执行的时候有很多like or like会影响效率。这么多朋友给了回复,感谢大家。我看大家给了2,3种方法,不知道大家认为哪种方法在效率上比较有优势呢?或者说从分析Oracle执行过程的角度分析分析谢谢。请高手们继续
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
*/
select * from tb
where code<'01' or code>'02zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
我觉得下面这种效率高一下;
select * from tb where code not like '01%' and code not like '02%';
其他的基本上没法使用索引;
where code<'01' or code>='03'like和in的性能并不好
oracle QQ群:54775466
欢迎爱好者 一起入群探讨
where
regexp_like(code,'^[^01|02]');
上面这个是什么函数啊
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')
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>