文章目录
- 1 准备数据
- 2 比较运算
- 3 逻辑运算
- 4 字符串like
- 5 in和not in
- 6 是否为空
- 7 between and
- 8 正则匹配
使用where语句可以按条件筛选数据
1 准备数据
商品明细数据
drop table if exists test.test_zw;
CREATE TABLE if not exists test.test_zw(
goods_name string COMMENT '商品名',
brand_name string COMMENT '品牌名',
spec string comment '规格',
price double comment '价格'
)
COMMENT '测试表'
STORED as parquet TBLPROPERTIES('parquet.compression'='SNAPPY');
-- 插入数据
insert into test.test_zw values
('伊利金典有机纯牛奶250ml*16盒','伊利','250ml*16盒',70),
('伊利无菌砖纯牛奶250ml*24盒','伊利','250ml*24盒',76.8),
('伊利纯牛奶180ml*16袋','伊利','',15.8),
('光明优加纯牛奶250ml*16盒','光明',null,44.9),
('光明有机纯牛奶200ml*24盒','光明','200ml*24盒',70),
('6月蒙牛特仑苏纯牛奶','蒙牛',null,47.9),
('蒙牛真果粒红柚四季春牛奶饮品饮料240g*12包','蒙牛','240g*12包',108);
查看所有数据
select goods_name,brand_name,price
from test.test_zw
2 比较运算
大于:选择价格大于70的商品
select goods_name,brand_name,price
from test.test_zw
where price >70
大于等于:选择价格大于等于70的商品
select goods_name,brand_name,price
from test.test_zw
where price >=70
等于:选择价格等于70的商品,注意是一个等号
select goods_name,brand_name,price
from test.test_zw
where price =70
不等于:选择价格不等于70的商品
select goods_name,brand_name,price
from test.test_zw
where price <>70
小于:选择价格小于70的商品
select goods_name,brand_name,price
from test.test_zw
where price <70
小于等于:选择价格小于等于70的数据
select goods_name,brand_name,price
from test.test_zw
where price <=70
3 逻辑运算
逻辑运算主要有或、且、非
或:使用or,选择价格小于50或者价格大于100的商品
select goods_name,brand_name,price
from test.test_zw
where price <50 or price >100
且:使用and,选择价格大于50且价格小于100的商品
select goods_name,brand_name,price
from test.test_zw
where price >50 and price <100
非:使用not,选择价格不大于50的商品
select goods_name,brand_name,price
from test.test_zw
where not price >50
4 字符串like
like可以进行字符串的模糊匹配,最重要是%的使用,%代表若干任意字符串。
选择商品名中包含’‘伊利’'的商品
select goods_name,brand_name,price
from test.test_zw
where goods_name like '%伊利%'
选择’伊利’开头的商品
select goods_name,brand_name,price
from test.test_zw
where goods_name like '伊利%'
选择”盒’'结尾的商品
select goods_name,brand_name,price
from test.test_zw
where goods_name like '%盒'
字符串模糊匹配是区分大小写的, 规格带小写ml的
select goods_name,brand_name,spec,price
from test.test_zw
where spec like '%ml%'
规格带大写ML的。
select goods_name,brand_name,spec,price
from test.test_zw
where spec like '%ML%'
5 in和not in
in和not in 可以方便的处理枚举类型。
选择品牌名为伊利和蒙牛的商品
select goods_name,brand_name,price
from test.test_zw
where brand_name in ('伊利','蒙牛')
选择品牌名不是伊利和蒙牛的商品
select goods_name,brand_name,price
from test.test_zw
where brand_name not in ('伊利','蒙牛')
6 是否为空
在SQL中,空值用NULL表示,判断是否为空要用is 或is not ,不能用等号和不等号
查找所有规格为空的数据
select goods_name,brand_name,spec,price
from test.test_zw
where spec is null
查找所有规格不为空的数据,注意那个空白是空字符串’’,不是NULL。
select goods_name,brand_name,spec,price
from test.test_zw
where spec is not null
查找规格为空字符串的数据
select goods_name,brand_name,spec,price
from test.test_zw
where spec =''
7 between and
between and 选取指定范围内的数据,包含两端数据,查询价格在70到108的商品
between and 选取指定范围内的数据,包含两端数据,查询价格在70到108的商品
select goods_name,brand_name,price
from test.test_zw
where price between 70 and 108
8 正则匹配
regexp在hive和mysql中都可以使用,匹配商品名称中包含伊利的数据
select goods_name,brand_name,spec,price
from test.test_zw
where goods_name regexp '伊利'
等价于
goods_name like '%伊利%'
Presto 使用的是regexp_like函数
select goods_name,brand_name,spec,price
from test.test_zw
where regexp_like(goods_name,'伊利')
看起来也没啥,但是当后面的关键字是变量时,优势就很明显了。比如在BI系统中,使用,可以接收用户输入的关键字进行模糊查询。正表达式的写法如下,like的写法就要想办法处理百分号的问题。
goods_name regexp $goods_name$
正则表达式,默认也是区分大小写的。只返回小写ml数据
select goods_name,brand_name,spec,price
from test.test_zw
where regexp_like(spec,'ml')
规格模糊匹配ml或者ML
select goods_name,brand_name,spec,price
from test.test_zw
where regexp_like(spec,'ml|ML')