13.Oracle数据库SQL开发之 SQL操作符

来源:互联网 发布:基层卫生网络直报 编辑:程序博客网 时间:2024/05/16 04:37

13.Oracle数据库SQL开发之 SQL操作符

     欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/49106979

    SQL操作符可以通过字符串或者值列表、值范围以及空值进行模式匹配,来限制查询返回的行。

可以使用NOT使一个操作符的含义相反。

1.  LIKE操作符

在WHERE子句中可以使用LIKE操作符来查看某一列中的字符串是否匹配指定的模式。

下划线_ 匹配指定位置的一个字符

百分号字符% 匹配从指定位置开始的任意个字符

‘_o%’ 表示第二个字母为o的字符串,如下

SQL> select * from customerswhere first_name like '_o%';

CUSTOMER_IDFIRST_NAME LAST_NAME  DOB     PHONE

--------------------- ---------- --------- ------------

          1 John       Brown  01-JAN-65 800-555-1211

          5 Doreen     Blue      20-MAY-70

使用NOT LIKE查询,结果如下:

SQL>select * from customers where first_name not like '_o%';

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           2 Cynthia   Green     05-FEB-68 800-555-1212

           3 Steve     White     16-MAR-71 800-555-1213

           4 Gail      Black                  800-555-1214

         如果需要对一个字符串中的下划线或百分号字符进行文本匹配,可以使用ESCAPE选项来标识这些字符。例如

‘%\%%’ ESCAPE ‘\’

第二个%比奥斯要搜索的实际字符。ESCAPE后面的字符高速数据库如何区分要搜索的字符与通配符。

例如:

SQL>select name from promotions where name like '%\%%' ESCAPE '\';

NAME

------------------------------

10% off Z Files

20% off Pop 3

30% off Modern Science

20% off Tank War

10% off Chemistry

20% off Creative Yell

15% off My Front Line

7 rows selected.

2.  IN操作符

在WHERE 子句中更可以使用IN操作符来检索其列值在某个列表中的行。

例如:

SQL> select * from customerswhere customer_id in (2,3,5);

 

CUSTOMER_IDFIRST_NAME LAST_NAME  DOB     PHONE

--------------------- ---------- --------- ------------

          2 Cynthia    Green    05-FEB-68 800-555-1212

          3 Steve      White    16-MAR-71 800-555-1213

          5 Doreen     Blue      20-MAY-70

使用NOT IN 检索哪些未被IN检索出来的行

SQL>select * from customers where customer_id not in (2,3,5);

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           1 John      Brown   01-JAN-65 800-555-1211

           4 Gail      Black                  800-555-1214

         如果包含空值,则NOTIN 返回FALSE。

3.  BETWEEN操作符

在WHERE子句中可以使用BETWEEN操作符来检索列值包含在指定区间内的行。

例如:

SQL>select * from customers where customer_id between 1 and 3;

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           1 John      Brown   01-JAN-65 800-555-1211

           2 Cynthia   Green     05-FEB-68 800-555-1212

           3 Steve     White     16-MAR-71 800-555-1213

使用NOT BWTWEEN检索如下:

SQL>select * from customers where customer_id not between 1 and 3;

 

CUSTOMER_ID FIRST_NAME LAST_NAME  DOB     PHONE

----------- ---------- ---------- ---------------------

           4 Gail      Black                  800-555-1214

           5 Doreen    Blue       20-MAY-70

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击