当前位置: 首页 > news >正文

数据库Mysql_联合查询

或许自己的不完美才是最完美的地方,那些让自己感到不安的瑕疵,最终都会变成自己的特色。

                                                                                                                                   ----------陳長生.


1.介绍

1.1.为什么要进行联合查询

        在数据设计的时候,由于范式的需求,会被分为多个表,但是当我们要查询数据时,单单一张表的内容是完全不够的,可能需要查询的数据覆盖多个表,那么我们就需要对表进行联合查询,也就是将多个表连接在一个表中。

1.2.如何实现联合查询

select * from 表1,表2;

select * from student,class;

将所有参与的表取他们的笛卡尔积,并将结果呈现在一张临时表中。

但是查询出来的结果有重复的值,我们需要对该表进行过滤

        可以看到,一个学生表中唐三藏的class_id对应多个班级表中的class_id,意思为原本属于1班的唐三藏现在属于1,2,3班,这种结果是不正确的,那我们在查询的时候就要声明一个班级只有一个唐三藏,其余学生也是一样,在MySql中我们称之为连接条件。

select * from student,class where student.class_id=class.class_id;

这样才是正确的结果

练习数据:

# 课程表

drop table if exists course;
create table if not exists course(
  course_id bigint primary key auto_increment,
  name varchar(20),
  student_id bigint 
);

# 分数表

drop table if exists score;
create table if not exists score(
  score double,
  student_id bigint,
  course_id bigint 
);

# 班级表

drop table if exists class;
create table if not exists class(
  class_id bigint primary key auto_increment,  
  name varchar(20),
  student_id bigint
);  

# 学生表

drop table if exists student;
create table if not exists student(
  id bigint primary key auto_increment,
  name varchar(20),
  sno bigint,
  age int,
  gender int,
  enroll_date varchar(20),
  class_id bigint
);


# 课程表
insert into course (name)values ('Java'), ('C++'), ('MySQL'), ('操作系统'), ('计算机网络'), ('数据结构');
# 班级表
insert into class(name) values ('Java001班'), ('C++001班'), ('前端001班');
# 学生表
insert into student (name, sno, age, gender, enroll_date, class_id) values 
('唐三藏', '100001', 18, 1, '1986-09-01', 1),
('孙悟空', '100002', 18, 1, '1986-09-01', 1),
('猪悟能', '100003', 18, 1, '1986-09-01', 1),
('沙悟净', '100004', 18, 1, '1986-09-01', 1),
('宋江', '200001', 18, 1, '2000-09-01', 2),
('武松', '200002', 18, 1, '2000-09-01', 2),
('李逹', '200003', 18, 1, '2000-09-01', 2),
('不想毕业', '200004', 18, 1, '2000-09-01', 2);
# 成绩表
insert into score (score, student_id, course_id) values
(70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),
(60, 2, 1),(59.5, 2, 5),
(33, 3, 1),(68, 3, 3),(99, 3, 5),
(67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),
(81, 5, 1),(37, 5, 5),
(56, 6, 2),(43, 6, 4),(79, 6, 6),
(80, 7, 2),(92, 7, 6);

2.内连接

1.select * from 表1 别名1,表2 别名2 where 连接条件 and 连接条件;(常用*)

2.select * from 表1 别名1,join 表2 别名2 on 连接条件 and 连接条件; 

2.1.查找武松的信息

select * from student s,class c where s.class_id=c.class_id and s.name='武松';

 select * from student s join class c on s.class_id=c.class_id and s.name='武松';

2.2.查询所有同学每门课的成绩以及同学的个人信息

select * from student s, score sc, course c 
where
s.id=sc.student_id
and
c.course_id=sc.course_id;

2.3.查询所有同学的总成绩以及同学的个人信息

select s.name,sum(sc.score) from student s,score sc 
where
s.id=sc.student_id
group by s.id;

3.外连接

外连接分别为左连接,右连接,全连接(MySql不支持全连接,所以这边就不讲了)

左连接:以左表为基础,将右表的数据插入左表中,若左表中有右表没有的数据,则插入的数据显示为NULL。

左连接:以右表为基础,将左表的数据插入右表中,若右表中有左表没有的数据,则插入的数据显示为NULL。

全连接:左表与右表的结合

注:不管是左表还是右表,MySql在执行的过程中都会转换为左连接,所以我们一般使用左连接

-- 左连接

select * from 左表 left join 右表 on 连接条件;

-- 右连接

select * from 左表 left join 右表 on 连接条件;

3.1.查询没有参加考试的同学信息

select * from student s(左表) 
left join
score sc (右表)
on 
s.id=sc.student_id(连接条件) 
where 
sc.score is null(判断条件);

3.2.查询没有学⽣的班级

select * from class c(左表) 
left join 
student s(右表)
on 
s.class_id=c.class_id(连接条件) 
where
s.id is nul(判断条件)l;

4.自连接

自连接就是自己与自己取笛卡尔积,可以把行转换为列,并在查询时过滤,就可以实现行与行之间的比较(MySql本身不支持行与行之间的比较,但是我们可以使用自连接实现)

select * from 表1 别名1,表1 别名2;

4.1.显示所有"MySQL"成绩⽐"JAVA"成绩⾼的成绩信息

select s1.* from 
score s1,
score s2,
course c1,
course c2

(将成绩表与课程表进行自连接)

where 
s1.student_id=s2.student_id
and 
s1.course_id=c1.course_id
and 
s2.course_id=c2.course_id

(过滤)
and 
c1.name='MySQL'
and 
c2.name='JAVA'
and 
s1.score>s2.score;

(根据题目要求进行条件判断)

5.子查询

子查询也叫嵌套查询,可以把一个查询的结果给另外一个查询语句当作条件

select * from 表名  where 条件=(select 条件 from 表名 ...);

5.1.单行子查询

5.1.1.查询与"不想毕业"同学的同班同学

-- 正常查询 (先查找不想毕业所在班级再通过班级找到同班同学)

select class_id from student where name='不想毕业' ;
select name from student where class_id=2;

-- 单行子查询(查询名字后,直接通过子查询判断class_id条件)

select name from student  

where

class_id=(select class_id from student where name='不想毕业' );

5.2.多行子查询

嵌套查询可以返回多行数据 ,使用in关键词

select * from 表名  where 条件  in(select 条件 from 表名 ...);

5.2.1.查询"MySQL"或"Java"课程的成绩信息

--正常查询

select course_id from course where name ='MySQL' or name='Java';
select * from score where course_id=1 or course_id=3 ;

--多行子查询

select * from score

where

course_id

in

(select course_id from course where name='MySQL' or name='Java'(多行数据));

5.3.多列子查询

5.3.1.查询重复录⼊的分数

先插入数据

insert into score values (70.5,1,1);

insert into score values (98.5,1,3);

insert into score values (99,3,5);

-- 多列子查询

select * from score where 
(score,student_id,course_id)
 in
  (select 
 score,student_id,course_id 
 from score
 group by 
 score,student_id,course_id 
 having count(*)>1);

5.4.在from子句中使用子查询

5.4.1.查询所有比"Java001班"平均分⾼的成绩信息

-- 先查平均分成绩

select avg(sc.score) score from student st, class c , score sc
where
st.class_id=c.class_id
and 
c.name='JAVA001班'
and 
st.id=sc.student_id;

--再查大于平均分

select * from score s,(select avg(sc.score) score from student st, class c , score sc
where
st.class_id=c.class_id
and 
c.name='JAVA001班'
and 
st.id=sc.student_id) temp
where 
s.score>temp.score;

6.合并查询

        合并查询分为 union 与union all,也就是将两张表的数据合并为一张存在临时表中,当前提是两张表的列信息一样。

6.1.union(去重)

select * from 表1 别名1 union select * from 表2 别名2;

6.2.union all(不去重)

select * from 表1 别名1 union all select * from 表2 别名2;

7.插入查询结果

        将一张表的数据插入到另一张表中,当前提是两个表的列信息一样。        

insert into 新表(列..)select ...

7.1.将student表中C++001班的学⽣复制到student1表中

-- 先创建新学生表(与原本的学生表列信息相同)

 create table new_student like student;

--复制

insert into 
new_student (name,sno,gender,enroll_date,class_id)
select 
s.name,s.sno,s.gender,s.enroll_date,s.class_id 
from 
student s,class c 
where 
s.class_id=c.class_id 
and 
c.name='C++001班' ;

相关文章:

  • ES6入门---第二单元 模块五:模块化
  • 去打印店怎么打印手机文件,网上打印平台怎么打印
  • flink常用算子整理
  • PyQt5基本介绍
  • MyBatis 核心类详解与架构解析:从入门到源码级理解
  • Kotlin协程解析
  • 网狐旗舰大联盟组件源码私测笔记:结构分层、UI重构与本地实操全流程
  • RockyLinux9.3-24小时制
  • RabbitMQ 深度解析:从核心组件到复杂应用场景
  • Docker 渡渡鸟镜像同步站 使用教程
  • 【SimSession 】3:中继服务 linux和windows实现及MFC集成实现
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】3.2 缺失值检测与处理(NULL值填充/删除策略)
  • 数理性能大幅提升|暴雨一体机适配DeepSeek Prover v2
  • 在 Ubuntu 系统中,查看已安装程序的方法
  • 广东省考备考(第一天5.4)—言语
  • 【KWDB 创作者计划】_KWDB 性能优化与调优
  • C++负载均衡远程调用学习之上报功能与存储线程池
  • iview table组件 自定义表头
  • 施磊老师rpc(四)
  • ASP.NET MVC​ 入门与提高指南九
  • 国防部新闻发言人就日本民用飞机侵闯中国钓鱼岛领空答记者问
  • “矿茅”国际化才刚开始?紫金矿业拟分拆境外黄金矿山资产于港交所上市
  • 五一假期天气将大转变,南方新一轮降雨来袭
  • 17家A股城商行一季报扫描:青岛银行营收增速领跑,杭州银行净利增速领跑
  • 《大风杀》上海首映,白客说拍这戏是从影以来的最大挑战
  • 国家卫健委对近日肖某引发舆情问题开展调查