面试问题:mysq,oracle,hive中对于null和空字符串“”,执行count,sum等函数的最终效果

使用同样的sql,分别在mysql,oracle,hive中执行,对比执行结果

Mysql
create table temp(tat varchar(10));
insert into temp values(null),(‘111’),(‘222’),(‘333’),(‘’);

a: select count(1) FROM temp;
b: select count(*) FROM temp;
c: select count(tat) FROM temp;
d: select sum(tat) from temp;
e: select avg(tat) from temp;
f: select min(tat) from temp;
g: select max(tat) from temp;
h: select * from temp order by tat desc;

结果:
a: 5
b: 5
c: 4
d: 666
e: 166.5(相当于是666/4)
f: ‘’(空字符串)
g: 333
h: 333,222,111,’’,(null)
结论:
可以看出在mysql中,count(字段),sum(),avg(),min(),max()都会自动过滤null值,但是不会过滤‘’空字符串,空字符串这一行参与了计算;

Oracle
create table temp(tat varchar2(10));
insert into temp values(‘’);
insert into temp values(null);
insert into temp (‘111’);
insert into temp (‘222’);
insert into temp (‘333’);

a: select count(1) FROM temp;
b: select count(*) FROM temp;
c: select count(tat) FROM temp;
d: select sum(tat) from temp;
e: select avg(tat) from temp;
f: select min(tat) from temp;
g: select max(tat) from temp;
h: select * from temp order by tat desc;

结果:
a: 5
b: 5
c: 3
d: 666
e: 222
f: 111
g: 333
h: (null),(null),333,222,111
结论:oracle数据库中,‘’空字符串被存储为了null,在执行函数
count(字段),sum(),avg(),min(),max(),会过滤null值,然后进行计算;

hive
create table temp(tat string));
insert into temp values(null),(‘111’),(‘222’),(‘333’),(‘’);

a: select count(1) FROM temp;
b: select count(*) FROM temp;
c: select count(tat) FROM temp;
d: select sum(tat) from temp;
e: select avg(tat) from temp;
f: select min(tat) from temp;
g: select max(tat) from temp;
h: select * from temp order by tat desc;

结果:
a: 5
b: 5
c: 4
d: 666
e: 166.5
f: ‘’(空字符串)
g: 333
h: 333,222,111,’’,null

结论:
针对字段的函数计算count(字段),sum(),avg(),min(),max()会去除null值,但不会去除‘’值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值