数据库Mysql_联合查询

#王者杯·14天创作挑战营·第1期#

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

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


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班' ;

MySQL数据库中,预处理和联合查询是两个重要的概念,它们在提高查询效率和灵活性方面起着关键作用。 ### 预处理 预处理(Prepared Statements)是一种在数据库中执行SQL语句的方法,它通过预编译SQL语句来提高性能和安全性和防止SQL注入攻击。预处理的步骤如下: 1. **准备语句**:首先,定义一个SQL语句模板,其中可以使用占位符(如`?`)来表示参数。 2. **绑定参数**:将实际的值绑定到占位符上。 3. **执行语句**:执行预处理语句。 例如: ```sql PREPARE stmt FROM 'SELECT * FROM users WHERE id = ?'; SET @id = 1; EXECUTE stmt USING @id; DEALLOCATE PREPARE stmt; ``` ### 联合查询 联合查询(JOIN)用于从两个或多个表中根据相关列的值来检索数据。常见的联合查询类型包括: 1. **INNER JOIN**:返回两个表中满足连接条件的记录。 2. **LEFT JOIN**:返回左表中的所有记录以及右表中满足连接条件的记录。 3. **RIGHT JOIN**:返回右表中的所有记录以及左表中满足连接条件的记录。 4. **FULL OUTER JOIN**:返回两个表中满足连接条件的记录以及左表或右表中不满足连接条件的记录。 例如: ```sql SELECT users.name, orders.order_id FROM users INNER JOIN orders ON users.id = orders.user_id; ``` ### 预处理和联合查询的结合使用 预处理和联合查询可以结合使用,以提高查询的安全性和效率。例如: ```sql PREPARE stmt FROM 'SELECT users.name, orders.order_id FROM users INNER JOIN orders ON users.id = orders.user_id WHERE users.id = ?'; SET @id = 1; EXECUTE stmt USING @id; DEALLOCATE PREPARE stmt;
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值