1,LIKE:检查列值是否匹配指定的模式;模式字符串中可以使用普通字符以及两个通配符:_和%。
_ 代表任意单个字符;
% 代表任意长度(长度可以为0)的字符串
因为_和%在模式字符串中具有特殊含义,因此如果要匹配这两个字符,可使用escape选项来标识这些字符。
select * from promotions t where name like '%\%%' escape '\ '
(将\声明为符号而不带入值中用于搜索)
注:NOT LIKE表示不匹配指定的模式。
当列值为空时,永远返回false。
eg:
SQL> select * from customers where first_name like '_o%';
CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE
----------- ---------- ---------- -------------- ------------
SQL> select * from customers where first_name like '_o';(o后面没有字母了)
未选定行
eg:
SQL> select * from customers where first_name like '%e%';
CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE
----------- ---------- ---------- -------------- ------------
SQL> select * from customers where last_name like '%e%';
CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE
----------- ---------- ---------- -------------- ------------
SQL> select * from customers where last_name like '%e%' and first_name like '%e%';(两个关键字都包含e的项目)
CUSTOMER_ID FIRST_NAME LAST_NAME DOB PHONE
----------- ---------- ---------- -------------- ------------
eg:
SQL> select * from promotions where name like '%\%%';(%% 效果一样)
PROMOTION_ID NAME DURATION
------------ ------------------------------ ---------------------------------------------------------------------------
2, 确定取值的集合:
SQL> select * from employees where employee_id in (2, 4);(将2行和4行的数值一起返回)
SQL> select * from employees where employee_id not in (2, 4);(除了2和4行的数值)
注意:如果值列表中含有null,则not in的运算结果永远为false,那么一条记录也不会被检索出来。如下:
select product_id,product_type_id,name
from products
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 where product_id between 8 and 10;
SQL> select * from products where name between 'A%' and 'Z%';(同样支持字符串 但是要将范围写清楚 日期也同样使用,但是格式要写清楚)
用于确定取值范围(闭区间)。
4, IS NULL 涉及空值的查询
“IS NULL” 不能用 “= NULL” 代替
IS NOT NULL
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 not nan;
6, IS INFINITE 判断列取值是否无穷大
IS NOT INFINITE