Day-4:高级数据过滤

1、组合WHERE子句

WHERE子句可以用AND或者OR来连接多个过滤条件。

  操作符:用来联结或者改变WHERE子句中的子句的关键字,也叫逻辑操作符。

AND操作符:前后两个条件都要满足

select prod_id, prod_price, prod_name
from products
where vend_id = 'DLL01' and prod_price <= 4;

/*
prod_id, prod_price, prod_name
BNBG01    3.49    Fish bean bag toy
BNBG02    3.49    Bird bean bag toy
BNBG03    3.49    Rabbit bean bag toy

*/

OR操作符:前后两个满足任意一个,第一满足就不会再计算第二个了

select prod_id, prod_price, prod_name
from products
where vend_id = 'DLL01' or vend_id = 'BRS01';

/*
prod_id, prod_price, prod_name
BNBG01    3.49    Fish bean bag toy
BNBG02    3.49    Bird bean bag toy
BNBG03    3.49    Rabbit bean bag toy
BR01    5.99    8 inch teddy bear
BR02    8.99    12 inch teddy bear
BR03    11.99    18 inch teddy bear
RGAN01    4.99    Raggedy Ann
*/

求值顺序

WHERE子句可以包含任意数目的AND和OR操作符。SQL先优先处理AND操作符。括号可以提高运算优先级。

例子:列出价格为10及以上,且由DLL01或BRS01制造的所有商品

select prod_price, prod_name
from products
where (vend_id = 'DLL01' or vend_id = 'BRS01') and prod_price >= 10;

/*
prod_price, prod_name
11.99    18 inch teddy bear
*/

IN操作符:指定范围,范围中的每个条件都可以进行匹配,后跟逗号分开的合法值,且必须在括号中。功能与OR相当。

select prod_name, prod_price
from products
where vend_id in ('DLL01','BRS01')
order by prod_name;

/*
prod_name, prod_price
12 inch teddy bear    8.99
18 inch teddy bear    11.99
8 inch teddy bear    5.99
Bird bean bag toy    3.49
Fish bean bag toy    3.49
Rabbit bean bag toy    3.49
Raggedy Ann    4.99
*/

NOT操作符:否定其后所跟的任何条件,NOT不单独使用(和其他操作符一起使用)

例子:列出除DLL01这外的所有供应商制造的产品

select prod_name
from products
where not vend_id = 'DLL01'  #where vend_id <> 'DLL01'
order by prod_name;

/*
prod_name
12 inch teddy bear
18 inch teddy bear
8 inch teddy bear
King doll
Queen doll
*/
原文地址:https://www.cnblogs.com/jp-mao/p/6560338.html