三.过滤数据
- where语句
- where语句操作符
= 等于
<>不等于
!=不等于
<=小于等于
!<不小于
between A and B 在A和B之间
is null 为null值
select prod_name,prod_price
from products
where prod_price = 3.19;
- 组合where子句
and操作符或or操作符
SQL处理逻辑中,and优先于or,所以若想让or优先要用圆括号括起来
select prod_id,prod_price,prod_name
from products
where vend_id = 'DLL01' and prod_price<=4;
(where vend_id = 'DLL01' or prod_price<=4;)
in操作符
select prod_name,prod_price
from products
where vend_id in ('DLL01','BRS01')
order by prod_name;
not操作符
用于否定其后的关键字
select prod_name,prod_price
from products
where not vend_id = ('DLL01','BRS01')
order by prod_name;
- like操作符
后接利用通配符匹配而不是简单的相等匹配的搜索模式
百分号通配符
%表示任何字符出现任意次数
select prod_id,prod_name
from products
where prod_name like'fish%'
#表示以fish起头的任意词,%表示接受fish之后的任意字符
select prod_id,prod_name
from products
where prod_name like'%fish%'
#表示包含fish的任何词,不管它前后出现什么
select prod_id,prod_name
from products
where prod_name like'f%s'
#表示以f开头以s结尾的词
- 下划线通配符
表示单个字符,用途与%一样
select prod_id,prod_name
from products
where prod_name like'_ inch teddy bear'
- 方括号通配符
指一个字符集
select cost_contact
from customers
where cust_contact like '[JM]%'
order by cust_contact
#以J或M打头的人名
select cost_contact
from customers
where cust_contact like '[^JM]%'
order by cust_contact
#非J或M打头的人名