mysql 筛选数据2

本文介绍了MySQL中常用的数据筛选操作,包括相关符号如等于、不等于、大于、小于的使用,以及高级筛选如AND、OR、WHERE、IN、NOT条件,还有通配符%和_的过滤方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相关符号筛选

= 等于 >大于 <小于
!= or <> 不等于
!< 不小于
!> 不大于
between and ,介于
is null ,空值

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 prod_name, prod_price
from products
where prod_price between 5 and 10;

# 检查是否存在空值, 这里不用select prod_price,因为我们不需要显示这一列,同时,需要注意的是无法通过null返回整行
select prod_name
from products
where prod_price is null;

select prod_name, prod_price
from products
where prod_price is null;

高级数据过滤 and or where in not

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

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

# 求值顺序 :处理or 之前,优先处理and, 但与一般算法相同,可以用括号来改变顺序
-- 先计算and
select prod_name, prod_price
from products
where vend_id = 'DLL01' or vend_id = 'BRS01' and prod_price >=10;

-- 先计算括号内的or
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') --效果同or,且优于or
order by prod_name;

# not 列出除了指定值的数据
select prod_name
from products
where not vend_id ='DLL01'
order by prod_name;

用通配符进行过滤

% 任意字符
_ 一个字符
[]指定位置的一个字符,按顺序筛选,mysql不支持

# %通配符:表示任意字符
select prod_id, prod_name 
from products
where prod_name like 'fish%';   -- Fish%表示,只要开头是Fish,后面是什么字符都可以
# mysql workbeach 是不区分大小写的,所以fish和Fish是一样的

# % 可以在任何位置使用
select prod_id, prod_name 
from products
where prod_name like '%bean bag%'; 

# 找以F开头,y结尾的所有产品
select prod_id, prod_name 
from products
where prod_name like 'F%y';  -- % 即使是0个字符也可以

# 排除条件
select prod_name, prod_desc
from products
where not prod_desc like '%toy%';

# _ : 表示单个字符
select prod_id, prod_name 
from products
where prod_name like '__ inch teddy bear';


# [] 通配符:指定位置的一个字符, [JM]就是匹配指定位置一个字符:优先匹配J,再匹配M
select cust_contact
from customers
where cust_contact like '[JM]%' # mysql不支持该功能
order by cust_contact;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值