hive-sql简单查询where筛选

使用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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f4OuzIjm-1625628809890)(picture/image-20210707112224794.png)]

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 ('伊利','蒙牛')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n1kfG4QZ-1625628809946)(picture/image-20210707113205355.png)]

选择品牌名不是伊利和蒙牛的商品

select goods_name,brand_name,price
from test.test_zw
where brand_name not in ('伊利','蒙牛')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KfFcYLpW-1625628809949)(picture/image-20210707113230457.png)]

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LI6HtGSP-1625628809956)(picture/image-20210707111622830.png)]

查找规格为空字符串的数据

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系统中,使用 g o o d s n a m e goods_name goodsname,可以接收用户输入的关键字进行模糊查询。正表达式的写法如下,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')

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hNg1etQM-1625639383049)(picture/image-20210707142846412.png)]

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值