当前位置: 代码迷 >> 综合 >> hive(数据库,表,数据增 查)
  详细解决方案

hive(数据库,表,数据增 查)

热度:50   发布时间:2023-12-03 01:29:37.0

数据库

1.创建数据库    create database 数据库名;

2.显示数据库    show databases;

3.删除数据库    drop database 数据库名;

1.创建表

create table 表名(字段名 字段类型,字段名 字段类型...)row format delimited fields terminated by '\t';

2.显示所有表   show tables;

3.删除表   drop table 表名;

数据

1.添加数据

load data (local) inpath '/数据文件地址.......' into table 表名;

2.查询

(1).查询所有

select * from 表名;

(2).按照字段查询

select 字段名,字段名2 from 表名;

(3).单一条件查询

select 字段名  from 表名 where 条件;

(4).全局排序

select 字段 from 表名 order by 排序字段 asc(升序,默认升序)/desc(降序);

(5).部分排序/分区排序  按照性别分组按照学号排序

select 字段 from 表名 distribute by sex sort by 学号;

(6).分组聚合   group by  各科目的最高分

select ... from 表名 group by 科目;按照xxx分组 group by后字段就是什么

聚合函数 最大值   max    最小值 min   求和  sum   求平均   avg     计数  count

(7).多表查询  找出各个表中相同的字段,这个就是连接字段

select 字段名.. from 表1,表2, 表n... where 表1.字段1 = 表2.字段x and 表2.字段x = 表n.字段n and ......;

select 字段 from 表1 join 表2 on 表1.字段1 = 表2.字段2 where xxxx;

left join 以左边为主表,如果右边的数据不存在,写null

right join 以右边为主表,如果左边的数据不存在,写null

(8).查询创建新表   CTAS 创建表

create table 表名 row format delimited fields terminated by '\t' as select xxxxxxxxxxxxxx;