首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive-1.2.1_05_案例操作

Hive-1.2.1_05_案例操作

作者头像
踏歌行
发布2020-10-15 11:33:38
3550
发布2020-10-15 11:33:38
举报
文章被收录于专栏:踏歌行的专栏踏歌行的专栏

1. 建库建表

 1 # 建库
 2 create database exercise;
 3 # 建表
 4 create table student(Sno int,Sname string,Sex string,Sage int,Sdept string)
 5  row format delimited fields terminated by ',';
 6 
 7 create table course(Cno int,Cname string) 
 8  row format delimited fields terminated by ',';
 9 
10 create table sc(Sno int,Cno int,Grade int)
11   row format delimited fields terminated by ',';
12 
13 # 查看有哪些表
14 hive (exercise)> show tables;
15 OK
16 course
17 sc
18 student
19 Time taken: 0.033 seconds, Fetched: 3 row(s)

2. 数据准备

相关数据

 1 [yun@mini01 exercise]$ pwd
 2 /app/software/hive/exercise
 3 [yun@mini01 exercise]$ ll /app/software/hive/exercise/
 4 total 12
 5 -rw-rw-r-- 1 yun yun  81 Jul 18 17:44 course.dat
 6 -rw-rw-r-- 1 yun yun 910 Jul 18 17:43 sc.dat
 7 -rw-rw-r-- 1 yun yun 527 Jul 18 17:42 students.dat
 8 [yun@mini01 exercise]$ cat /app/software/hive/exercise/course.dat 
 9 1,数据库
10 2,数学
11 3,信息系统
12 4,操作系统
13 5,数据结构
14 6,数据处理
15 [yun@mini01 exercise]$ cat /app/software/hive/exercise/sc.dat 
16 95001,1,81
17 95001,2,85
18 95001,3,88
19 95001,4,70
20 95002,2,90
21 95002,3,80
22 95002,4,71
23 95002,5,60
24 95003,1,82
25 95003,3,90
26 95003,5,100
27 95004,1,80
28 95004,2,92
29 95004,4,91
30 95004,5,70
31 95005,1,70
32 95005,2,92
33 95005,3,99
34 95005,6,87
35 95006,1,72
36 95006,2,62
37 95006,3,100
38 95006,4,59
39 95006,5,60
40 95006,6,98
41 95008,1,98
42 95008,3,89
43 95008,6,91
44 95010,2,98
45 95010,5,90
46 95010,6,80
47 95011,1,81
48 95011,2,91
49 95011,3,81
50 95011,4,86
51 95012,1,81
52 95012,3,78
53 95012,4,85
54 95012,6,98
55 95013,1,98
56 95013,2,58
57 95013,4,88
58 95013,5,93
59 [yun@mini01 exercise]$ cat /app/software/hive/exercise/students.dat 
60 95001,李勇,男,20,CS
61 95002,刘晨,女,19,IS
62 95003,王敏,女,22,MA
63 95004,张立,男,19,IS
64 95005,刘刚,男,18,MA
65 95006,孙庆,男,23,CS
66 95007,易思玲,女,19,MA
67 95008,李娜,女,18,CS
68 95009,梦圆圆,女,18,MA
69 95010,孔小涛,男,19,CS

数据导入

 1 # desc formatted course;
 2 # desc formatted sc;
 3 # desc formatted student;
 4 # 查看location信息
 5 # 数据导入
 6 0: jdbc:hive2://mini01:10000> load data local inpath '/app/software/hive/exercise/course.dat' into table course; # 导入数据
 7 INFO  : Loading data to table exercise.course from file:/app/software/hive/exercise/course.dat
 8 INFO  : Table exercise.course stats: [numFiles=1, totalSize=81]
 9 No rows affected (0.35 seconds)
10 0: jdbc:hive2://mini01:10000> load data local inpath '/app/software/hive/exercise/sc.dat' into table sc; # 导入数据
11 INFO  : Loading data to table exercise.sc from file:/app/software/hive/exercise/sc.dat
12 INFO  : Table exercise.sc stats: [numFiles=1, totalSize=910]
13 No rows affected (0.25 seconds) 
14 0: jdbc:hive2://mini01:10000> load data local inpath '/app/software/hive/exercise/students.dat' into table student; # 导入数据
15 INFO  : Loading data to table exercise.student from file:/app/software/hive/exercise/students.dat
16 INFO  : Table exercise.student stats: [numFiles=1, totalSize=527]
17 No rows affected (0.381 seconds)

3. 常用操作

3.1. 学生表基本查询

查询学号为95001, 95005, 95008的学生信息

1 select * from student a where a.sno in(95001, 95005, 95008);
2 +--------+----------+--------+---------+----------+--+
3 | a.sno  | a.sname  | a.sex  | a.sage  | a.sdept  |
4 +--------+----------+--------+---------+----------+--+
5 | 95001  | 李勇       | 男      | 20      | CS       |
6 | 95005  | 刘刚       | 男      | 18      | MA       |
7 | 95008  | 李娜       | 女      | 18      | CS       |
8 +--------+----------+--------+---------+----------+--+
9 3 rows selected (0.076 seconds)

查询学号中有9500字符串的学生信息

 1 select * from student a where a.sno like '%9500%';
 2 +--------+----------+--------+---------+----------+--+
 3 | a.sno  | a.sname  | a.sex  | a.sage  | a.sdept  |
 4 +--------+----------+--------+---------+----------+--+
 5 | 95001  | 李勇       | 男      | 20      | CS       |
 6 | 95002  | 刘晨       | 女      | 19      | IS       |
 7 | 95003  | 王敏       | 女      | 22      | MA       |
 8 | 95004  | 张立       | 男      | 19      | IS       |
 9 | 95005  | 刘刚       | 男      | 18      | MA       |
10 | 95006  | 孙庆       | 男      | 23      | CS       |
11 | 95007  | 易思玲      | 女      | 19      | MA       |
12 | 95008  | 李娜       | 女      | 18      | CS       |
13 | 95009  | 梦圆圆      | 女      | 18      | MA       |
14 +--------+----------+--------+---------+----------+--+
15 9 rows selected (0.081 seconds)

查询全体学生的学号与姓名

 1 select a.Sno, a.Sname from student a;
 2 +--------+----------+--+
 3 | a.sno  | a.sname  |
 4 +--------+----------+--+
 5 | 95001  | 李勇       |
 6 | 95002  | 刘晨       |
 7 | 95003  | 王敏       |
 8 | 95004  | 张立       |
 9 | 95005  | 刘刚       |
10 | 95006  | 孙庆       |
11 | 95007  | 易思玲      |
12 | 95008  | 李娜       |
13 | 95009  | 梦圆圆      |
14 | 95010  | 孔小涛      |
15 +--------+----------+--+
16 10 rows selected (0.085 seconds)

查询学生的总人数

1 select count(distinct sno) count from student;
2 +--------+--+
3 | count  |
4 +--------+--+
5 | 10     |
6 +--------+--+
7 1 row selected (21.355 seconds)

3.2. 成绩表相关查询

查询选修了课程的学生姓名

 1 # 表和表字段,大小写不明感   distinct 去重复
 2 select distinct a.sno, a.sname from student a inner join sc b on a.sno = b.sno;
 3 +--------+----------+--+
 4 | a.sno  | a.sname  |
 5 +--------+----------+--+
 6 | 95001  | 李勇       |
 7 | 95002  | 刘晨       |
 8 | 95003  | 王敏       |
 9 | 95004  | 张立       |
10 | 95005  | 刘刚       |
11 | 95006  | 孙庆       |
12 | 95008  | 李娜       |
13 | 95010  | 孔小涛      |
14 +--------+----------+--+
15 8 rows selected (32.694 seconds)

计算1号课程的学生平均成绩

1 select avg(a.grade) from sc a where a.cno = 1;
2 +--------------------+--+
3 |        _c0         |
4 +--------------------+--+
5 | 82.55555555555556  |
6 +--------------------+--+
7 1 row selected (20.72 seconds)

查询各科成绩平均分

 1 select a.cno, avg(a.grade) from sc a group by a.cno;
 2 +--------+--------------------+--+
 3 | a.cno  |        _c1         |
 4 +--------+--------------------+--+
 5 | 1      | 82.55555555555556  |
 6 | 2      | 83.5               |
 7 | 3      | 88.125             |
 8 | 4      | 78.57142857142857  |
 9 | 5      | 78.83333333333333  |
10 | 6      | 90.8               |
11 +--------+--------------------+--+
12 6 rows selected (20.084 seconds)

查询选修1号课程的学生最高分数【查课程和分数,不要学生信息】

1 select a.cno, a.grade from sc a where a.cno = 1 order by a.grade desc limit 1;
2 +--------+----------+--+
3 | a.cno  | a.grade  |
4 +--------+----------+--+
5 | 1      | 98       |
6 +--------+----------+--+
7 1 row selected (19.005 seconds)

3.3. 函数查询

求各个课程号及相应的选课人数

 1 select a.cno, count(a.sno) from sc a group by a.cno;
 2 +--------+------+--+
 3 | a.cno  | _c1  |
 4 +--------+------+--+
 5 | 1      | 9    |
 6 | 2      | 8    |
 7 | 3      | 8    |
 8 | 4      | 7    |
 9 | 5      | 6    |
10 | 6      | 5    |
11 +--------+------+--+
12 6 rows selected (20.967 seconds)

查询选修了3门以上的课程的学生学号

 1 select * from (select a.sno, count(a.cno) countCno from sc a group by a.sno) x where x.countCno > 3;
 2 或 select a.sno, count(a.cno) countCno from sc a group by a.sno having countCno > 3;
 3 或 select a.sno, count(a.cno) countCno from sc a group by a.sno having count(a.cno) > 3;
 4 +--------+-------------+--+
 5 | x.sno  | x.countcno  |
 6 +--------+-------------+--+
 7 | 95001  | 4           |
 8 | 95002  | 4           |
 9 | 95004  | 4           |
10 | 95005  | 4           |
11 | 95006  | 6           |
12 | 95011  | 4           |
13 | 95012  | 4           |
14 | 95013  | 4           |
15 +--------+-------------+--+
16 8 rows selected (19.556 seconds)

查询学生信息,结果按学号全局有序

 1 select * from student a order by a.sno;
 2 +--------+----------+--------+---------+----------+--+
 3 | a.sno  | a.sname  | a.sex  | a.sage  | a.sdept  |
 4 +--------+----------+--------+---------+----------+--+
 5 | 95001  | 李勇       | 男      | 20      | CS       |
 6 | 95002  | 刘晨       | 女      | 19      | IS       |
 7 | 95003  | 王敏       | 女      | 22      | MA       |
 8 | 95004  | 张立       | 男      | 19      | IS       |
 9 | 95005  | 刘刚       | 男      | 18      | MA       |
10 | 95006  | 孙庆       | 男      | 23      | CS       |
11 | 95007  | 易思玲      | 女      | 19      | MA       |
12 | 95008  | 李娜       | 女      | 18      | CS       |
13 | 95009  | 梦圆圆      | 女      | 18      | MA       |
14 | 95010  | 孔小涛      | 男      | 19      | CS       |
15 +--------+----------+--------+---------+----------+--+
16 10 rows selected (18.736 seconds)

查询学生信息,按性别分区,在分区内按年龄有序

 1 # 设置map数量
 2 0: jdbc:hive2://mini01:10000> set mapreduce.job.reduces = 2;
 3 No rows affected (0.007 seconds)
 4 0: jdbc:hive2://mini01:10000> set mapreduce.job.reduces;
 5 +--------------------------+--+
 6 |           set            |
 7 +--------------------------+--+
 8 | mapreduce.job.reduces=2  |
 9 +--------------------------+--+
10 1 row selected (0.01 seconds)
11 ### 或者 **************
12 0: jdbc:hive2://mini01:10000> set mapred.reduce.tasks;
13 +-------------------------+--+
14 |           set           |
15 +-------------------------+--+
16 | mapred.reduce.tasks=-1  |
17 +-------------------------+--+
18 1 row selected (0.008 seconds)
19 0: jdbc:hive2://mini01:10000> set mapred.reduce.tasks=2;
20 No rows affected (0.004 seconds)
21 0: jdbc:hive2://mini01:10000> set mapred.reduce.tasks;
22 +------------------------+--+
23 |          set           |
24 +------------------------+--+
25 | mapred.reduce.tasks=2  |
26 +------------------------+--+
27 1 row selected (0.008 seconds)
28 ##################################################################
29 # 具体操作
30 select * from student a distribute by a.sex sort by a.sage;
31 +--------+----------+--------+---------+----------+--+
32 | a.sno  | a.sname  | a.sex  | a.sage  | a.sdept  |
33 +--------+----------+--------+---------+----------+--+
34 | 95005  | 刘刚       | 男      | 18      | MA       |
35 | 95010  | 孔小涛      | 男      | 19      | CS       |
36 | 95004  | 张立       | 男      | 19      | IS       |
37 | 95001  | 李勇       | 男      | 20      | CS       |
38 | 95006  | 孙庆       | 男      | 23      | CS       |
39 | 95009  | 梦圆圆      | 女      | 18      | MA       |
40 | 95008  | 李娜       | 女      | 18      | CS       |
41 | 95007  | 易思玲      | 女      | 19      | MA       |
42 | 95002  | 刘晨       | 女      | 19      | IS       |
43 | 95003  | 王敏       | 女      | 22      | MA       |
44 +--------+----------+--------+---------+----------+--+
45 10 rows selected (22.323 seconds)

3.4. 多表查询

查询每个学生及其选修课程的情况

 1 select a.sno, a.sname, c.cno, c.cname from 
 2 student a inner join sc b on a.sno = b.sno
 3 inner join course c on b.cno = c.cno
 4 order by a.sno, c.cno;
 5 +--------+----------+--------+----------+--+
 6 | a.sno  | a.sname  | c.cno  | c.cname  |
 7 +--------+----------+--------+----------+--+
 8 | 95001  | 李勇       | 1      | 数据库      |
 9 | 95001  | 李勇       | 2      | 数学       |
10 | 95001  | 李勇       | 3      | 信息系统     |
11 | 95001  | 李勇       | 4      | 操作系统     |
12 | 95002  | 刘晨       | 2      | 数学       |
13 | 95002  | 刘晨       | 3      | 信息系统     |
14 | 95002  | 刘晨       | 4      | 操作系统     |
15 | 95002  | 刘晨       | 5      | 数据结构     |
16 | 95003  | 王敏       | 1      | 数据库      |
17 | 95003  | 王敏       | 3      | 信息系统     |
18 | 95003  | 王敏       | 5      | 数据结构     |
19 | 95004  | 张立       | 1      | 数据库      |
20 | 95004  | 张立       | 2      | 数学       |
21 | 95004  | 张立       | 4      | 操作系统     |
22 | 95004  | 张立       | 5      | 数据结构     |
23 | 95005  | 刘刚       | 1      | 数据库      |
24 | 95005  | 刘刚       | 2      | 数学       |
25 | 95005  | 刘刚       | 3      | 信息系统     |
26 | 95005  | 刘刚       | 6      | 数据处理     |
27 | 95006  | 孙庆       | 1      | 数据库      |
28 | 95006  | 孙庆       | 2      | 数学       |
29 | 95006  | 孙庆       | 3      | 信息系统     |
30 | 95006  | 孙庆       | 4      | 操作系统     |
31 | 95006  | 孙庆       | 5      | 数据结构     |
32 | 95006  | 孙庆       | 6      | 数据处理     |
33 | 95008  | 李娜       | 1      | 数据库      |
34 | 95008  | 李娜       | 3      | 信息系统     |
35 | 95008  | 李娜       | 6      | 数据处理     |
36 | 95010  | 孔小涛      | 2      | 数学       |
37 | 95010  | 孔小涛      | 5      | 数据结构     |
38 | 95010  | 孔小涛      | 6      | 数据处理     |
39 +--------+----------+--------+----------+--+
40 31 rows selected (26.356 seconds)

查询学生的得分情况

 1 select a.sno, a.sname, c.cno, c.cname, b.grade from 
 2 student a inner join sc b on a.sno = b.sno
 3 inner join course c on b.cno = c.cno
 4 order by a.sno, c.cno;
 5 +--------+----------+--------+----------+----------+--+
 6 | a.sno  | a.sname  | c.cno  | c.cname  | b.grade  |
 7 +--------+----------+--------+----------+----------+--+
 8 | 95001  | 李勇       | 1      | 数据库      | 81       |
 9 | 95001  | 李勇       | 2      | 数学       | 85       |
10 | 95001  | 李勇       | 3      | 信息系统     | 88       |
11 | 95001  | 李勇       | 4      | 操作系统     | 70       |
12 | 95002  | 刘晨       | 2      | 数学       | 90       |
13 | 95002  | 刘晨       | 3      | 信息系统     | 80       |
14 | 95002  | 刘晨       | 4      | 操作系统     | 71       |
15 | 95002  | 刘晨       | 5      | 数据结构     | 60       |
16 | 95003  | 王敏       | 1      | 数据库      | 82       |
17 | 95003  | 王敏       | 3      | 信息系统     | 90       |
18 | 95003  | 王敏       | 5      | 数据结构     | 100      |
19 | 95004  | 张立       | 1      | 数据库      | 80       |
20 | 95004  | 张立       | 2      | 数学       | 92       |
21 | 95004  | 张立       | 4      | 操作系统     | 91       |
22 | 95004  | 张立       | 5      | 数据结构     | 70       |
23 | 95005  | 刘刚       | 1      | 数据库      | 70       |
24 | 95005  | 刘刚       | 2      | 数学       | 92       |
25 | 95005  | 刘刚       | 3      | 信息系统     | 99       |
26 | 95005  | 刘刚       | 6      | 数据处理     | 87       |
27 | 95006  | 孙庆       | 1      | 数据库      | 72       |
28 | 95006  | 孙庆       | 2      | 数学       | 62       |
29 | 95006  | 孙庆       | 3      | 信息系统     | 100      |
30 | 95006  | 孙庆       | 4      | 操作系统     | 59       |
31 | 95006  | 孙庆       | 5      | 数据结构     | 60       |
32 | 95006  | 孙庆       | 6      | 数据处理     | 98       |
33 | 95008  | 李娜       | 1      | 数据库      | 98       |
34 | 95008  | 李娜       | 3      | 信息系统     | 89       |
35 | 95008  | 李娜       | 6      | 数据处理     | 91       |
36 | 95010  | 孔小涛      | 2      | 数学       | 98       |
37 | 95010  | 孔小涛      | 5      | 数据结构     | 90       |
38 | 95010  | 孔小涛      | 6      | 数据处理     | 80       |
39 +--------+----------+--------+----------+----------+--+
40 31 rows selected (24.469 seconds)

查询选修2号课程且成绩在90分以上的所有学生

 1 select a.sno, a.sname, b.cno, b.grade
 2 from student a inner join sc b on a.sno = b.sno
 3 where b.cno = 2 and b.grade >= 90;
 4 +--------+----------+--------+----------+--+
 5 | a.sno  | a.sname  | b.cno  | b.grade  |
 6 +--------+----------+--------+----------+--+
 7 | 95002  | 刘晨       | 2      | 90       |
 8 | 95004  | 张立       | 2      | 92       |
 9 | 95005  | 刘刚       | 2      | 92       |
10 | 95010  | 孔小涛      | 2      | 98       |
11 +--------+----------+--------+----------+--+
12 4 rows selected (16.728 seconds)

查询所有学生的信息,如果在成绩表中有成绩,则输出成绩表中的课程号和成绩

  如果student的sno值对应的sc在中没有值,则会输出student.Sname null.如果用right out join会保留右边的值,左边的为null。
  Join 发生在WHERE 子句之前。如果你想限制 join 的输出,应该在 WHERE 子句中写过滤条件——或是在join 子句中写。
 1 select a.sno, a.sname, b.cno, b.grade
 2 from student a left join sc b on a.sno = b.sno;
 3 +--------+----------+--------+----------+--+
 4 | a.sno  | a.sname  | b.cno  | b.grade  |
 5 +--------+----------+--------+----------+--+
 6 | 95001  | 李勇       | 1      | 81       |
 7 | 95001  | 李勇       | 2      | 85       |
 8 | 95001  | 李勇       | 3      | 88       |
 9 | 95001  | 李勇       | 4      | 70       |
10 | 95002  | 刘晨       | 2      | 90       |
11 | 95002  | 刘晨       | 3      | 80       |
12 | 95002  | 刘晨       | 4      | 71       |
13 | 95002  | 刘晨       | 5      | 60       |
14 | 95003  | 王敏       | 1      | 82       |
15 | 95003  | 王敏       | 3      | 90       |
16 | 95003  | 王敏       | 5      | 100      |
17 | 95004  | 张立       | 1      | 80       |
18 | 95004  | 张立       | 2      | 92       |
19 | 95004  | 张立       | 4      | 91       |
20 | 95004  | 张立       | 5      | 70       |
21 | 95005  | 刘刚       | 1      | 70       |
22 | 95005  | 刘刚       | 2      | 92       |
23 | 95005  | 刘刚       | 3      | 99       |
24 | 95005  | 刘刚       | 6      | 87       |
25 | 95006  | 孙庆       | 1      | 72       |
26 | 95006  | 孙庆       | 2      | 62       |
27 | 95006  | 孙庆       | 3      | 100      |
28 | 95006  | 孙庆       | 4      | 59       |
29 | 95006  | 孙庆       | 5      | 60       |
30 | 95006  | 孙庆       | 6      | 98       |
31 | 95007  | 易思玲      | NULL   | NULL     |
32 | 95008  | 李娜       | 1      | 98       |
33 | 95008  | 李娜       | 3      | 89       |
34 | 95008  | 李娜       | 6      | 91       |
35 | 95009  | 梦圆圆      | NULL   | NULL     |
36 | 95010  | 孔小涛      | 2      | 98       |
37 | 95010  | 孔小涛      | 5      | 90       |
38 | 95010  | 孔小涛      | 6      | 80       |
39 +--------+----------+--------+----------+--+
40 33 rows selected (17.467 seconds)

查询与“王敏”在同一个系学习的学生

 1 select a.sno, a.sname
 2 from student a inner join student b
 3 on a.sdept = b.sdept and b.sname = '王敏';
 4 +--------+----------+--+
 5 | a.sno  | a.sname  |
 6 +--------+----------+--+
 7 | 95003  | 王敏       |
 8 | 95005  | 刘刚       |
 9 | 95007  | 易思玲      |
10 | 95009  | 梦圆圆      |
11 +--------+----------+--+
12 4 rows selected (16.954 seconds)

4. 各种连接查询

4.1. 数据准备

 1 [yun@mini01 exercise2]$ cat /app/software/hive/exercise2/a.dat 
 2 1,a
 3 2,b
 4 3,c
 5 4,d
 6 7,y
 7 8,u
 8 [yun@mini01 exercise2]$ cat /app/software/hive/exercise2/b.dat 
 9 2,bb
10 3,cc
11 7,yy
12 9,pp

4.2. 建表并导入数据

1 # 建表
2 # 使用的库和上面的一样,所以就不单独建库了
3 create table a(id int,name string)
4 row format delimited fields terminated by ',';
5 
6 create table b(id int,name string)
7 row format delimited fields terminated by ',';
1 # 导入数据
2 load data local inpath '/app/software/hive/exercise2/a.dat' into table a;
3 load data local inpath '/app/software/hive/exercise2/b.dat' into table b;
 1 # 表数据查询
 2 0: jdbc:hive2://mini01:10000> select * from a;
 3 +-------+---------+--+
 4 | a.id  | a.name  |
 5 +-------+---------+--+
 6 | 1     | a       |
 7 | 2     | b       |
 8 | 3     | c       |
 9 | 4     | d       |
10 | 7     | y       |
11 | 8     | u       |
12 +-------+---------+--+
13 6 rows selected (0.083 seconds)
14 0: jdbc:hive2://mini01:10000> select * from b;
15 +-------+---------+--+
16 | b.id  | b.name  |
17 +-------+---------+--+
18 | 2     | bb      |
19 | 3     | cc      |
20 | 7     | yy      |
21 | 9     | pp      |
22 +-------+---------+--+
23 4 rows selected (0.071 seconds)

4.3. 各种各种join查询

1 select * from a inner join b on a.id=b.id;
2 select * from a left join b on a.id=b.id;
3 select * from a right join b on a.id=b.id;
4 select * from a full outer join b on a.id=b.id;
5 # 类似 inner join 但是只取左表的信息
6 select * from a left semi join b on a.id = b.id;

具体如下:

1 select * from a inner join b on a.id=b.id;
2 +-------+---------+-------+---------+--+
3 | a.id  | a.name  | b.id  | b.name  |
4 +-------+---------+-------+---------+--+
5 | 2     | b       | 2     | bb      |
6 | 3     | c       | 3     | cc      |
7 | 7     | y       | 7     | yy      |
8 +-------+---------+-------+---------+--+
9 3 rows selected (17.714 seconds)
 1 select * from a left join b on a.id=b.id;
 2 +-------+---------+-------+---------+--+
 3 | a.id  | a.name  | b.id  | b.name  |
 4 +-------+---------+-------+---------+--+
 5 | 1     | a       | NULL  | NULL    |
 6 | 2     | b       | 2     | bb      |
 7 | 3     | c       | 3     | cc      |
 8 | 4     | d       | NULL  | NULL    |
 9 | 7     | y       | 7     | yy      |
10 | 8     | u       | NULL  | NULL    |
11 +-------+---------+-------+---------+--+
12 6 rows selected (16.359 seconds)
 1 select * from a right join b on a.id=b.id;
 2 +-------+---------+-------+---------+--+
 3 | a.id  | a.name  | b.id  | b.name  |
 4 +-------+---------+-------+---------+--+
 5 | 2     | b       | 2     | bb      |
 6 | 3     | c       | 3     | cc      |
 7 | 7     | y       | 7     | yy      |
 8 | NULL  | NULL    | 9     | pp      |
 9 +-------+---------+-------+---------+--+
10 4 rows selected (22.34 seconds)
 1 select * from a full outer join b on a.id=b.id;
 2 +-------+---------+-------+---------+--+
 3 | a.id  | a.name  | b.id  | b.name  |
 4 +-------+---------+-------+---------+--+
 5 | 1     | a       | NULL  | NULL    |
 6 | 2     | b       | 2     | bb      |
 7 | 3     | c       | 3     | cc      |
 8 | 4     | d       | NULL  | NULL    |
 9 | 7     | y       | 7     | yy      |
10 | 8     | u       | NULL  | NULL    |
11 | NULL  | NULL    | 9     | pp      |
12 +-------+---------+-------+---------+--+
13 7 rows selected (24.746 seconds)
1 select * from a left semi join b on a.id = b.id;
2 +-------+---------+--+
3 | a.id  | a.name  |
4 +-------+---------+--+
5 | 2     | b       |
6 | 3     | c       |
7 | 7     | y       |
8 +-------+---------+--+
9 3 rows selected (19.147 seconds)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 建库建表
  • 2. 数据准备
  • 3. 常用操作
    • 3.1. 学生表基本查询
      • 3.2. 成绩表相关查询
        • 3.3. 函数查询
          • 3.4. 多表查询
          • 4. 各种连接查询
            • 4.1. 数据准备
              • 4.2. 建表并导入数据
                • 4.3. 各种各种join查询
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档