3.3join 查询
将多个表通过字段关联在一起,形成查询结果
创建 student_location 表,存储学生的地理位置信息
CREATE TABLE student_location( id string comment \'stdno\',
province string comment \'province name\', city string comment \'city name\',
region string comment \'region name\'
)
comment \'student location info\' ROW FORMAT DELIMITED
FIELDS TERMINATED BY \'\\t\' LINES TERMINATED BY \'\\n\'
构造 student_location 表数据
001 河北省 石家庄市 高新区
将数据加载进 student_location 表中
LOAD DATA LOCAL INPATH \'./student_location.txt\' OVERWRITE INTO TABLE student_location;
join 查询示例
inner join
将左表和右表满足联接条件的数据,全部查询出来
select * from student
inner join student_location
on student.id=student_location.id;
left outer join
以左表为主,将左表数据全部保留,没有关联上数据字段置成 NULL
select * from student
left outer join student_location
on student.id=student_location.id;
right outer join
以右表为主,将右表数据全部保留,没有关联上数据字段置成 NULL
select * from student
right outer join student_location
on student.id=student_location.id;
full outer join
没有关联上数据字段全部置成 NULL
select * from student
full outer join student_location
on student.id=student_location.id;
3.4union
union all
将所有表数据,完全叠加在一起,不去重。要求:所有表的字段和类型完全一致。
以 student 和 student_outer 表为例
select * from student
union all
select * from student_outer;
union
将所有表数据,完全叠加在一起,总体去重。要求:所有表的字段和类型完全一致。
以 student 和 student_outer 表为例
select * from student
union
select * from student_outer;
继续阅读与本文标签相同的文章
下一篇 :
NoSQL之Redis
-
给编程菜鸟的16条忠告,你做到几条
2026-05-16栏目: 教程
-
带你读《增长密码:大型网站百万流量运营之道》之一:引言
2026-05-16栏目: 教程
-
带你读《Flutter技术入门与实战》之三:Dart语言简述
2026-05-16栏目: 教程
-
带你读《增长密码:大型网站百万流量运营之道》之二:营销世界里SEO的重要性
2026-05-16栏目: 教程
-
SpringBoot的yml配置及多环境切换
2026-05-16栏目: 教程
