【Hive---09】运算符『 关系运算符 | 算术运算符 | 逻辑运算符 | 括号 | null | case end』

1. 关系运算符

每个关系运算符都返回 boolean 类型结果:

在这里插入图片描述

注意:判断字段值是否为null,不能用=或者!=,只能用col is nullcol is not null

1.1 is not null

--is null空值判断
select 1 from dual where 'itcast' is null; -- 返回0条数据
--is not null 非空值判断
select 1 from dual where 'itcast' is not null; -- 返回一条数据,且数值为1

在这里插入图片描述

1.2 like

like比较: _表示任意单个字符, %表示任意数量字符。

select 1 from dual where 'itcast' like 'it_'; -- 返回0条数据1
select 1 from dual where 'itcast' like 'it%'; -- 返回一条数据,且数值为

-- not...like 中的not 可以放在like前;也可以放在where后。下面两条语句等价:
select 1 from dual where 'itcast' not like 'hadoo_';
select 1 from dual where not 'itcast' like 'hadoo_';

1.3 rlike / regexp

rlike / regexp:确定字符串是否匹配正则表达式。

select 1 from dual where 'itcast' rlike '^i.*t$'; --- 返回1条数据,且值为1
select 1 from dual where '123456' rlike '^\\d+$'; --判断是否全为数字.返回1条数据,且值为1
select 1 from dual where '123456aa' rlike '^\\d+$'; -- 返回0条数据

-- 可以把rlike关键字换成regexp,效果是一样的。
select 1 from dual where 'itcast' regexp '^i.*t$';

2. 算术运算符

每个算术运算符都返回

在这里插入图片描述

  1. 直接在字段后进行四则运算:

    select 1+1 from table1; -- 返回1条数据,且值为 2
    select 1+1; -- hive3.0之后可以不写 from 表名
    
  2. 直接在字段前面加上 负号 ,可以将数变为相反数:
    在这里插入图片描述

接下来重点介绍右边的。

2.1 div 和 %

div:取计算结果的整数部分,其实就是向下取整
%:取计算结果的余数

select 17 div 3; -- 返回一条数据,且值为 5
select 17 % 3; -- 返回一条数据,且值为 2

2.2 ~、& 、 | 、^

~:某数按位取反
&:两数进行 ’与‘ 逻辑操作
| :两数进行 ’或‘ 逻辑操作
^:两数进行 ’异或‘ 逻辑操作

--4转换二进制:0100 6转换二进制:0110
select 6 from dual; -- 返回一条数据
select 6 & 4 from dual; -- 返回一条数据,且值为 4
select 6 | 4 from dual; -- 返回一条数据,且值为 6
select 6 ^ 4 from dual; -- 返回一条数据,且值为 2

3. 逻辑运算符

在这里插入图片描述

select 1 from dual where 3 > 1 and 2 > 1; -- 返回一条数据,且值为1
select 1 from dual where 3 > 1 or 2 != 2; -- 返回一条数据,且值为1
select 1 from dual where not 2 > 1; -- 返回0条数据
select 1 from dual where ! 2=1; -- 返回一条数据,且值为1

-- 在不在
select 1 from dual where 11 in(11,22,33); -- 返回一条数据,且值为1
select 1 from dual where 11 not in(22,33,44); -- 返回一条数据,且值为1

-- 存不存在:[NOT] EXISTS (subquery)
select A.* from A
where exists (select B.id from B where A.id = B.id);

4. 和java一样,括号内的先运算

5. case … end

  1. 语法:

    case
    	when colName布尔表达式 thenwhen colName布尔表达式 then....  
    	elseend as colName_rename
    
  2. 功能:

    1. 等于 if-else if-else
    2. 等价实现or的功能:用到or时经常伴随着要用很多括号,导致看的很不方便,此时可以用 case…end 等价替换,去掉括号
      在这里插入图片描述
  3. 细节:如果有多个when..then...,如果一条记录满足第一个case when就不会再去执行后面的case when。和java的if-else if-else一样,当做java使用就可以了
    在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ElegantCodingWH

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

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

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

打赏作者

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

抵扣说明:

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

余额充值