Oracle like 模糊查询


LIKE 模糊查询

字符匹配操作可以使用通配符 “%” 和 “_”:

%:表示任意个字符,包括零个;

_:表示一个任意字符;

  1. Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0    
  2. Connected as scott   
  3.   
  4. SQL> select * from dept;   
  5.     
  6. DEPTNO DNAME          LOC   
  7. ------ -------------- -------------   
  8.     10 ACCOUNTING     NEW YORK   
  9.     20 RESEARCH       DALLAS   
  10.     30 SALES          CHICAGO   
  11.     40 OPERATIONS     BOSTON  

"%" 和 "_"演示:

  1. SQL> select * from dept where DNAME like '_A%';   
  2.     
  3. DEPTNO DNAME          LOC   
  4. ------ -------------- -------------   
  5.     30 SALES          CHICAGO  

ESCAPE 演示:

  1. SQL> insert into dept values(50,'BEIJING','JIANG%XI');   
  2.     
  3. 1 row inserted   
  4.     
  5. SQL> select * from dept;   
  6.     
  7. DEPTNO DNAME          LOC   
  8. ------ -------------- -------------   
  9.     10 ACCOUNTING     NEW YORK   
  10.     20 RESEARCH       DALLAS   
  11.     30 SALES          CHICAGO   
  12.     40 OPERATIONS     BOSTON   
  13.     50 BEIJING        JIANG%XI   
  14.   
  15.  SQL> select * from dept where loc like '%\%%' escape '\';   
  16.     
  17. DEPTNO DNAME          LOC   
  18. ------ -------------- -------------   
  19.     50 BEIJING        JIANG%XI   
  20.   
  21.  SQL> select * from dept where loc like '%e%%' escape 'e';   
  22.     
  23. DEPTNO DNAME          LOC   
  24. ------ -------------- -------------   
  25.     50 BEIJING        JIANG%XI   

更多Oracle相关信息见Oracle 专题页面 http://www.bkjia.com/topicnews.aspx?tid=12

相关内容