第4-6课 数据的过滤

实际查询中,通常不会检索所有行,需要对数据进行筛选过滤,选出符合我们需要条件的数据。

sql中的数据过滤通过where子句中指定的搜索条件进行

where子句操作符

检查单个值

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 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_name
from products
where prod_name is null;

组合where子句

and or操作符

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

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');

not 操作符

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

使用通配符进行过滤

使用like操作符进行通配搜索

%表示字符任意出现的次数,fish开头的字符

select prod_id,prod_name
from products
where prod_name like 'Fish%';

与%类似,但只匹配单个字符

select prod_id,prod_name
from products
where prod_name like '__ inch teddy bear';

[]通配符用来匹配字符集,必须匹配方括号中的某一个字符

select cust_contact
from customers
where cust_contact like '[JM]%';
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容