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)]

### HiveSQL 查询技巧与最佳实践 #### 优化查询性能 为了提高查询效率,应当尽可能减少返回的数据量。例如,在涉及大量数据的情况下,可以利用子查询筛选特定条件下的记录[^2]。 ```sql SELECT a.* FROM transactions a WHERE a.user_id IN ( SELECT user_id FROM users WHERE signup_date > '2021-01-01' ); ``` 通过这种方式能够有效降低处理的数据规模,从而加快执行速度。 #### 利用内置函数和自定义函数增强功能 除了标准的 SQL 函数外,还可以创建临时函数以扩展系统的计算能力。这使得复杂业务逻辑可以直接嵌入到查询语句中完成,而无需额外编写程序代码[^3]。 ```sql CREATE TEMPORARY FUNCTION toprovince AS 'cn.itcast.bigdata.udf.ToProvince'; ``` 此命令注册了一个名为 `toprovince` 的 UDF(用户定义函数),可用于转换或解析字段内容。 #### 合理规划表结构设计 当构建新表格时,应考虑分区(partitioning) 和分桶(bucketing),这两种机制有助于加速读取操作以及节省存储空间。对于频繁访问的部分数据集来说尤为重要。 #### 数据倾斜问题解决方法 如果遇到某些 key 对应过多 value 导致的任务运行缓慢,则可以通过调整 map/reduce 参数设置或者重新分配 keys 来缓解这种情况;另外也可以尝试采用随机前缀法打散热点key分布。 #### 善于运用视图简化复杂查询 建立视图可以帮助隐藏底层物理表的具体实现细节,并提供更加简洁直观的操作接口给最终使用者。这对于维护大型项目中的多层关联关系特别有用处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值