1、WHERE
查询产品名为book的所有产品的名称:
select prod_name from products where prod_name = 'book';
查询产品价格小于10的所有产品的名称和价格:
select prod_name, prod_price from products where prod_price < 10;
查询产品价格在5到10之间的所有产品的名称和价格 :
select prod_name, prod_price from products where prod_price between 5 and 10;
查询没有产品价格的所有产品的名称和价格 :
select prod_name, prod_price from products where prod_price is null;
2、组合WHERE子句(and \ in\ not in)
查询供应商1003制造且价格小于等于10美元的所有产品的名称和价格
select prod_price, prod_name from products where vend_id=1003 and prod_price<=10;
查询任一个供应商1002或者1003制造的所有产品的产品名和价格
select prod_price, prod_name from products where vend_id=1003 or vend_id=1002;
select prod_price, prod_name from products where vend_id in (1002,1003) ;
查询出除1002和1003之外的所有供应商制造的产品
select prod_price, prod_name from products where vend_id not in (1002,1003) ;
查询价格为10美元(含)以上且由1002或1003制造的所有产品
select prod_name from products where vend_id=1003 or vend_id=1002 and prod_price >= 10;
3、通配符过滤(LIKE)
%通配符匹配任意(除NULL)
select prod_name from products where prod_name like 'jet%';
_下划线的用途与%一样,但下划线只匹配单个字符而不是多个字符
select prod_name from products where prod_name like '_ton anvil';
select prod_name from products where prod_name like '%ton anvil';
![]()
4、正则表达式
句检索列prod_name包含 文本1000的所有行:
select prod_name from products where prod_name regexp '1000' order by prod_name;
select prod_name from products where prod_name regexp '.000' order by prod_name;
为搜索两个串之一(或者为这个串,或者为另一个串),使用|
select prod_name from products where prod_name regexp '1000|2000' order by prod_name;
通过指定一组用[和]括起来的字符来完成
select prod_name from products where prod_name regexp '[123] ton' order by prod_name;
[123]定义一组字符,它 的意思是匹配1或2或3
为了匹配特殊字符,必须用\\(转义字符)为前导。\\-表示查找-,\\.表示查找.
select vend_name from vendors where vend_name regexp '\\.' order by vend_name ;
目前为止使用的所有正则表达式都试图匹配单次出现。如果存在一个匹配,该行被检索出来,如果不存在,检索不出任何行。但有时需要对匹配的数目进行更强的控制。例如,你可能需要寻找所有的数,不管数中包含多少数字,或者你可能想寻找一个单词并且还能够适应一个尾随的s(如果存在),等等。