0


Hive(二)

修改表的结构:

重命名:

alter table table_1 rename to table_2;

增加列信息:
alter table table_1 add columns (mycol string,mysco string);

更新列:
alter table table_1 change column mycol int;

删除表:

drop table table_1;

数据查询:

查询所有列(全表查询):

select * from score;

查询特定列:

select s_id,c_id from table_1;

常用函数:

求总行数(count):

select count(1) from score;

求分数最大数(max);

select max(s_score) from score;

求分数最小值(min):

select min(s_score) from score;

求分数总和(sum):

select sum(s_scote) from score;

求分数平均值:

select avg(s_score) from score;

limilt语句:

限制返回的行数:

select * from score limit 3;(返回三行)

where语句:

用于条件限制:

例:查询分数大于80的学生的学号:

select stu_num from students where contrnt>80;

like语句:

选择类似的值

% 代表任意个字符

_ 代表一个字符

例:查询姓李的学生的学号:

select num_stu from students where name like '李%';

例:寻找姓李的单名学生的学号:

select num_stu from students where name like '李_';

逻辑运算符

and or not

例:查询不姓王的学生姓名:

select name from students where name not '王%';

分组与having语句

分组语句group by 语句,可以按照一个或多个队列的结果进行分组,然后进行操作

select s_id,avg(s_sorce) from score group by s_id;

having语句:

只针对表中的列发挥作用,where后面不能写分组函数,having后面可以使用分组函数

having只用于group by 分组统计语言

例:求每个学生的平均分数:

select s_id ,avg(s_sorce) from score group by s_id;

求每个学生平均分数大于85的人:

select s_id ,avg(s_sorce) avgsore from score

group by s_id having avgsore > 85;

join操作

支持等值连接,不支持非等值连接

例:查询分数对应的科目名

select s.s_id,s.s_score,stu.s_name,stu.s_birth from score s join student stu on s.s_id = stu.s_id;

合并表:

select * from teacher join course on teacher.t_id = course.t_id;

内连接:

select * from teacher inner join course on tercher.t_id = course.t_id;

左连接:

select * from teacher legt join course on teacher.t_id = course.t_id;

右连接:

select * from teacher right join course on teacher.t_id = course.t_id;

多表连接:

select * from teacher

join course on teacher.t_id = course.t_id

select * from student

join course on teacher.t_id = student.t_id;

排序:

Order By

默认升序排列(ascend)(ASC)

降序:(descend)(DESC)

例:查询学生成绩,按照分数降序排列:

select * from student left join score on students.s_id = score.s_id order by sco.s_score DESC;

查询学生成绩,按照分数升序排列:

select * from student left join score on students.s_id = score.s_id order by sco.s_score ASC;

按照分数平均值排序:

select s_id avg(s_score) avg from score group by s_id order by avg;

常用内置函数:

字符串连接函数:

concat

select concat(‘abc’,‘def’,‘gh’); 'abcdefgh'

将小写字母转化为大写字母:
upper

select upper(‘asd’); 'ASD'

自定义函数:

UDF(一进一出)

UDAF(多进一出)

UDTF(一进多出)


本文转载自: https://blog.csdn.net/qq_63781342/article/details/135607276
版权归原作者 ꧁༺朝花夕逝༻꧂ 所有, 如有侵权,请联系我们删除。

“Hive(二)”的评论:

还没有评论