SQL 操作符

来源:互联网 发布:mac口红免税店价格 编辑:程序博客网 时间:2024/06/15 16:20
1,LIKE:检查列值是否匹配指定的模式;模式字符串中可以使用普通字符以及两个通配符:_和%。
 代表任意单个字符;
代表任意长度(长度可以为0)的字符串

因为_和%在模式字符串中具有特殊含义,因此如果要匹配这两个字符,可使用escape选项来标识这些字符。

select *from promotions t where name like '%\%%' escape '\'
(将\声明为符号而不带入值中用于搜索)

注:NOTLIKE表示不匹配指定的模式。
当列值为空时,永远返回false。

eg:

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

CUSTOMER_IDFIRST_NAME LAST_NAME  DOB          PHONE                     
--------------------- ---------- -------------- ------------               
         1 John      Brown     01-1月 -65    800-555-1211               
         5 Doreen    Blue      20-5月 -70                              

SQL>select * from customers where first_name like'_o';(o后面没有字母了)

未选定行

eg:

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

CUSTOMER_IDFIRST_NAME LAST_NAME  DOB          PHONE                     
--------------------- ---------- -------------- ------------               
         3 Steve     White     16-3月 -71    800-555-1213               
         5 Doreen    Blue      20-5月 -70                              

SQL>select * from customers where last_name like'%e%';

CUSTOMER_IDFIRST_NAME LAST_NAME  DOB          PHONE                     
--------------------- ---------- -------------- ------------               
         2 Cynthia   Green     05-2月 -68    800-555-1212               
         3 Steve     White     16-3月 -71    800-555-1213               
         5 Doreen    Blue      20-5月 -70                              

SQL>select * from customers where last_name like '%e%' and first_namelike '%e%';(两个关键字都包含e的项目)

CUSTOMER_IDFIRST_NAME LAST_NAME  DOB          PHONE                     
--------------------- ---------- -------------- ------------               
         3 Steve     White     16-3月 -71    800-555-1213               
         5 Doreen    Blue      20-5月 -70                              

eg:

SQL>select * from promotions where name like '%\%%';(%%效果一样)

PROMOTION_IDNAME                       DURATION                                    
---------------------------------------------------------------------------------------------------------------------                                                     
         6 \test                      +00302:25:45.0000 


2, 确定取值的集合:
    IN(…,…,…) 圆括号中是用逗号分隔的一组取值。

SQL>select * from employees where employee_id in (2,4);(将2行和4行的数值一起返回)

    NOT IN (…,…,…) 和IN操作结果相反。

SQL>select * from employees where employee_id not in (2,4);(除了2和4行的数值)

注意:如果值列表中含有null,则notin的运算结果永远为false,那么一条记录也不会被检索出来。如下:
selectproduct_id,product_type_id,name 
fromproducts 

eg:

SQL>where product_type_id not in (1,2,null);
未选定行
(若无not 只是1,2,null 则若有数值则返回数值)
注:select对于列进行选择 where 对于行进行选择

附加的小尾巴 
1=1的用法小实例

sql = select* from student where 1=1

if(sid !=null){sql += " and sid = 123";}

if(name !=null){sql += " and name like '%name%'";}

说明

这样子写就可以省略下面繁琐的搜索了
select *from student where sid = 123;
select *from student where name like '%name%';
select *from student where age = 22;

3, BETWEEN … AND  
(包括左边和右边的闭区间)
eg:
SQL>select * from products whereproduct_id between 8 and 10;

SQL>select * from products where name between 'A%' and 'Z%';(同样支持字符串但是要将范围写清楚 日期也同样使用,但是格式要写清楚)

    NOTBETWEEN  …  AND 
用于确定取值范围(闭区间)。

4, IS NULL涉及空值的查询
“IS NULL”不能用 “= NULL” 代替
IS NOTNULL

SQL>select * from customers where dob is null;

下面两个判断用于BINARY_FLOAT和BINARY_DOUBLE类型的列:
5,  用于判断列取值是否非数字

IS NAN 是非数字- 不是数字

IS NOT NAN不是非数字-是数字

SQL>select * from products where product_type_id is notnan;

6, ISINFINITE  判断列取值是否无穷大
IS NOTINFINITE

0 0
原创粉丝点击