基本where子句
-
where子句操作符
=(等于),<>(不等于),!=(不等于),<(小于),<=(小于等于),>(大于),>=(大于等于),between a and b(在a和b之间);
select prod_name,prod_price
from products
where prod_price<10;
select prod_name
from products
where prod_name is NULL;
组合where子句
select prod_name,prod_price
from products
where vend_id=1002 or vend_id=1003 and prod_price>=10;//优先处理and
select prod_name,prod_price
from products
where vend_id in (1002,1003)//相当于OR
order by prod_name;
select prod_name,prod_price
from products
where vend_id not in (1002,1003)
order by prod_name;
使用通配符进行过滤
- 通配符(wildcard):用来匹配值得特殊字符
- 搜索模式(search pattern):由字面值、通配符或两者组合构成的搜索条件
- 在搜索子句中使用通配符必须使用LIKE操作符
- 常用通配符
- ‘%’:表示任何字符出现任意次数(0次,1次或多次),不匹配NULL。‘jet%’表示以jet开头的字符(不区分大小写)。
select prod_id,prod_name
from products
where prod_name like '%anvil%';//表示文本中任意位置包含anvil
- ‘_’:匹配单个字符
- 通配符不可过度使用,使用期搜索比较慢
- 将通配符置于搜索模式的开始处搜索起来最慢
使用正则表达式进行过滤
- 正则表达式:用来匹配文本的特殊的串
- 使用关键字REGEXP
- 常用正则表达式
- 关键字LIKE与REGEXP的区别:LIKE匹配整个列,REGEXP匹配列值,只要出现就匹配
select prod_name
from products
where prod_name like '1000';//没有返回
select prod_name
from products
where prod_name like '1000';//返回JetPack 1000
网友评论