实际查询中,通常不会检索所有行,需要对数据进行筛选过滤,选出符合我们需要条件的数据。
sql中的数据过滤通过where子句中指定的搜索条件进行
where子句操作符
检查单个值
select prod_name, prod_price
from products
where prod_price = 3.49;
select prod_name,prod_price
from products
where prod_price < 10;
select vend_id, prod_name
from products
where vend_id <> 'DLL01';
select vend_id, prod_name
from products
where vend_id != 'DLL01';
范围值检查
select prod_name,prod_price
from products
where prod_price between 5 and 10;
检查空值
select prod_name
from products
where prod_name is null;
组合where子句
and or操作符
select prod_name, prod_price
from products
where vend_id = 'DLL01' OR vend_id = 'BRS01'
and prod_price >= 10;
select prod_name, prod_price
from products
where (vend_id = 'DLL01' OR vend_id = 'BRS01')
and prod_price >= 10;
in操作符
select prod_name, prod_price
from products
where vend_id in( 'DLL01' , 'BRS01');
not 操作符
select prod_name
from products
where not vend_id = 'DLL01'
order by prod_name;
使用通配符进行过滤
使用like操作符进行通配搜索
%表示字符任意出现的次数,fish开头的字符
select prod_id,prod_name
from products
where prod_name like 'Fish%';
与%类似,但只匹配单个字符
select prod_id,prod_name
from products
where prod_name like '__ inch teddy bear';
[]通配符用来匹配字符集,必须匹配方括号中的某一个字符
select cust_contact
from customers
where cust_contact like '[JM]%';