MySQL过滤数据

使用where子句

检查单个值

select prod_price, prod_name 
from products 
where prod_name = 'fuses';
select prod_price, prod_name 
from products 
where prod_price <= 10;

不匹配检查

select prod_price, prod_name 
from products 
where prod_price != 10;

范围值检查

select prod_price, prod_name 
from products 
where prod_price between 5 and 10;

空值检查

select prod_price, prod_name 
from products 
where prod_price is null;

组合where子句

and操作符

select prod_price, prod_name 
from products 
where vend_id = 1003 and prod_price <= 10;

or操作符

select prod_price, prod_name 
from products 
where vend_id = 1002 or vend_id = 1003; 

计算次序

select prod_price, prod_name 
from products 
where (vend_id = 1002 or vend_id = 1003) and prod_price>=10; 

in操作符

select prod_price, prod_name 
from products 
where vend_id in (1002, 1003)  
order by prod_name;

not操作符

select prod_price, prod_name 
from products 
where vend_id not in (1002, 1003)  
order by prod_name;

like操作符

百分号通配符(匹配0~多个字符,NULL除外)

select prod_price, prod_name 
from products 
where prod_name like 'jet%';

下划线通配符(只匹配一个字符)

select prod_price, prod_name 
from products 
where prod_name like '_ ton anvil';

正则表达式

基本字符匹配:检索列prod_name包含文本1000的所有行

select prod_name 
from products 
where prod_name regexp '1000' 
order by prod_name;

进行or匹配:

select prod_name 
from products 
where prod_name regexp '1000|2000' 
order by prod_name;

匹配几个字符之一:[123]表示匹配1或2或3,即[1|2|3]

select prod_name 
from products 
where prod_name regexp '[123] Ton' 
order by prod_name;

匹配范围:[1-5]表示匹配1到5

select prod_name 
from products 
where prod_name regexp '[1-5] Ton' 
order by prod_name;

匹配特殊字符:.、|、[]等前面加\

select prod_name 
from products 
where prod_name regexp '\\.' 
order by prod_name;

匹配多个实例:匹配连在一起的4个数字

select prod_name 
from products 
where prod_name regexp '[[:digit:]]{4}' 
order by prod_name;

定位符:找出一个数(包括以小数点开始的数)开始的所有产品

select prod_name 
from products 
where prod_name regexp '^[0-9\\.]' 
order by prod_name;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给算法爸爸上香

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值