SQL过滤数据


查询中存在一些没有用到的数据时,我们需要对数据进行过滤;
使用 where 函数对数据进行过滤

where + 数学符号

数学符号包括:
> , < , = , <=, >= , >< (不等于)

select 
	purchase_price
from 
	product
where
	purchase_price = 500;  -- 这里的500 也可以是字符

举例1:
在这里插入图片描述
举例2:
在这里插入图片描述

where + between … and …

between 边界1 and 边界2 ;
注意:包含边界

select 
	purchase_price
from 
	product
where
	purchase_price between	790 and 2800;
	

在这里插入图片描述

where + is null

查询表格为空的记录

select 
	product_name,
    product_type,
    purchase_price
from 
	product
where
	 purchase_price is null;

在这里插入图片描述

where + 逻辑运算符

where + and

使用逻辑运算可以限制过个条件

select 
	product_name,
    product_type,
    purchase_price
from 
	product
where
	 (purchase_price is null) and (product_type = '厨房用具');

在这里插入图片描述

where + or

select 
	product_name,
    product_type,
    purchase_price
from 
	product
where
	 (purchase_price is null) 
     or (product_type = '厨房用具');

在这里插入图片描述

where + and + or

适当的使用括号,可以避免逻辑运算符的错误。

select 
	product_name,
    product_type,
    purchase_price
from 
	product
where
	 (purchase_price is null) 
     or (product_type = '厨房用具' 
		 and purchase_price between 790 and 2800);
		 

在这里插入图片描述

where + not + in

找出和条件不匹配的记录

select
	product_name,
    product_type,
    purchase_price
from 
	product
where
	not product_type in ( '厨房用具'); 
	

在这里插入图片描述

where + in + 某类别下的内容

in 表示我们限制了要使用哪些数据; in 后面的内容用 小括号括起来

select
	product_name,
    product_type,
    purchase_price
from 
	product
where
	product_name in ('菜刀', '叉子');

在这里插入图片描述

where + in + sql语句查询出来的内容

select
	product_name,
    product_type,
    purchase_price
from 
	product
where
	product_name in (
		select 
			product_name
		from 
			product
		where 
			product_type in ('厨房用具')
    );

在这里插入图片描述

where + like + 通配符

select
	product_name,
    product_type,
    purchase_price
from 
	product
where
	product_name like '%T恤';
	

在这里插入图片描述
通配符说明:
% 可以匹配满足条件的所有字符,只要存在就输出
_ 下划线,仅仅匹配单个字符,几个下划线就匹配几个字符;

注意:
通配符不能匹配 null

where + regexp + 正则表达式

举例1:

select
	product_name,
    product_type,
    purchase_price
from 
	product
where
	product_name regexp '^T恤';  -- 查询以T恤开头的信息

在这里插入图片描述
举例2:

select
	product_name,
    product_type,
    purchase_price
from 
	product
where
	product_name regexp '^[^[T恤]]';  -- 查询不以这个开头的信息
	

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值