相关符号筛选
= 等于 >大于 <小于
!= or <> 不等于
!< 不小于
!> 不大于
between and ,介于
is null ,空值
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 prod_name, prod_price
from products
where prod_price between 5 and 10;
# 检查是否存在空值, 这里不用select prod_price,因为我们不需要显示这一列,同时,需要注意的是无法通过null返回整行
select prod_name
from products
where prod_price is null;
select prod_name, prod_price
from products
where prod_price is null;
高级数据过滤 and or where in not
# and
select prod_id, prod_price, prod_name
from products
where vend_id = 'DLL01' and prod_price <=4;
# or
select prod_price, prod_name
from products
where vend_id = 'DLL01' or vend_id = 'BRS01';
# 求值顺序 :处理or 之前,优先处理and, 但与一般算法相同,可以用括号来改变顺序
-- 先计算and
select prod_name, prod_price
from products
where vend_id = 'DLL01' or vend_id = 'BRS01' and prod_price >=10;
-- 先计算括号内的or
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') --效果同or,且优于or
order by prod_name;
# not 列出除了指定值的数据
select prod_name
from products
where not vend_id ='DLL01'
order by prod_name;
用通配符进行过滤
% 任意字符
_ 一个字符
[]指定位置的一个字符,按顺序筛选,mysql不支持
# %通配符:表示任意字符
select prod_id, prod_name
from products
where prod_name like 'fish%'; -- Fish%表示,只要开头是Fish,后面是什么字符都可以
# mysql workbeach 是不区分大小写的,所以fish和Fish是一样的
# % 可以在任何位置使用
select prod_id, prod_name
from products
where prod_name like '%bean bag%';
# 找以F开头,y结尾的所有产品
select prod_id, prod_name
from products
where prod_name like 'F%y'; -- % 即使是0个字符也可以
# 排除条件
select prod_name, prod_desc
from products
where not prod_desc like '%toy%';
# _ : 表示单个字符
select prod_id, prod_name
from products
where prod_name like '__ inch teddy bear';
# [] 通配符:指定位置的一个字符, [JM]就是匹配指定位置一个字符:优先匹配J,再匹配M
select cust_contact
from customers
where cust_contact like '[JM]%' # mysql不支持该功能
order by cust_contact;