【MySQL数据库】详细讲解MySQL的查询

数据库技术
209
0
0
2024-02-08
标签   MySQL语句

🍔多表查询

⭐多表关系

在项目开发中,在进行数据库表结构设计时,会根据业务需求以及业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在各种联系,基本分为以下三种

  • 一对多(多对一)
  • 多对多
  • 一对一

🎈一对多(多对一)

案例:部门和员工之间的关系 关系:一个部门对于多个员工,一个员工对应一个部门

🏀实现

的一方建立外键,指向的一方的主键

🎈多对多

案例:学生与课程之间的关系 关系:一个学生可以选修多门课程,一门课程也可以供多个学生选择

🏀实现

建立第三张中间表,中间表至少包含两个外键分别关联两方主键

🏳️‍🌈创建两个表并且插入数据

create table student(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
) comment '学生表';
​
insert into student values (null,'小吉','123'),(null,'fufu','123'),(null,'御坂美琴','123'),(null,'海绵宝宝','123');

create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
) comment '课程表';
​
insert into course values (null,'java'),(null,'php'),(null,'mysql'),(null,'c嘎嘎');

🏳️‍🌈创建第三个表 通过第三个表来维护他们之间的关系

create table student_course(
    id int auto_increment comment '主键' primary key,
    studentid int not null comment '学生ID',
    courseid  int not null comment '课程ID',
    constraint fk_courseid foreign key (courseid) references course(id),
    constraint fk_studentid foreign key (studentid) references course(id)
)comment '学生课程之间表';
​
insert into student_course values (null,1,1),(null,1,2),(null,1,3),(null,2,2),(null,2,3),(null,3,4);

🏀以图形化的方式来表示

🎈一对一

案例:用户与用户详情的关系 关系:一对一关系,多用于单表拆分,将一张表的基础字段放在一张表中,其他详情字段放在另一张表中,以提升操作效率

🏀实现:

在任意一方加入外键,关联另外一方的主键,并且设置外键为唯一的(unique)

先建立表

create table tb_user(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '姓名',
    age int comment '年龄',
    gender char(1) comment '1: 男 , 2:女 ',
    phone char(11) comment '手机号'
) comment '用户基本信息表';

先建立表

create table tb_user_edu(
    id int auto_increment primary key comment '主键ID',
    degree varchar(20) comment '学历',
    major varchar(20) comment '专业',
    primaryschool varchar(50) comment '小学',
    middleschool varchar(50) comment '中学',
    university varchar(50) comment '大学',
    userid int unique comment '用户ID',
    constraint fk_userid foreign key (userid) references tb_user(id)
)comment '用户教育信息表';

然后向表中插入数据

insert into tb_user(id, name, age, gender, phone) values
        (null,'小吉','1','2','3'),
        (null,'小吉','1','2','3'),
        (null,'小吉','1','2','3'),
        (null,'小吉','1','2','3');
​
​
insert into tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid) values
         (null,'博士','计算机','1','1','哔哩哔哩大学',1),
         (null,'博士','计算机','1','1','哔哩哔哩大学',2),
         (null,'博士','计算机','1','1','哔哩哔哩大学',3),
         (null,'博士','计算机','1','1','哔哩哔哩大学',4);
一条userid就对应一个用户的基本信息

🏀以图形化的方式展示

⭐多表查询语法

select * from1,表2,……;

🎈单表查询

🎈多表查询

🏀我们发现,最终的结果有16条,但是是为什么呢

因为44=16 course中有4条,student中有4条,44=16 这种现象称为笛卡儿积

🏳️‍🌈笛卡儿积

是指在数学中,集合A和集合B的所有的组成情况

当前就是两张表所有的组成情况 但是在设计开发中,我们不需要这么多的情况,我们需要消除无效的情况

🏀消除无效的笛卡儿积
select * from course ,student where course.id=student.id;

⭐内连接

🎈隐式内连接

select 字段列表 from1,表2 where 条件……;
查询学生的姓名以及关联的课程
select student.name,course.name from student,course where student.id=course.id;

🎈显示内连接

select 字段列表 from1 (inner) join2 on 连接条件……;
查询学生的姓名以及关联的课程
select student.name,course.name from student inner join  course on student.id=course.id;

⭐外连接

🎈左外连接

🤖相当于查询左表的所有数据 包含 左表和右表交集部分的数据

select 字段列表 from1 left (outer) join2 on 条件……;
会查询到左表的所有数据

左表

🎈右外连接

🤖相当于查询右表的所有数据 包含 左表和右表交集部分的数据

select 字段列表 from1 right (outer) join2 on 条件……;
会查询到右表的所有数据

⭐自连接

就是在同一张表中进行查询 需要把一张表看作两张表

自连接必须起别名

原来的表

🌺联合查询

对于联合查询(union),就是把多次查询的结果合并起来,形成一个新的查询结果集

查询到的多张表的列数要保持一致,而且字段列表也要保持一致
select 字段列表 from 表A……
union (all)
select 字段列表 from 表B……;

🎈union all

合并查询到的两个表,不进行查重

🎈union

合并查询到的两个表,进行查重

这里我们发现,“鹿杖客”出现了两次 那么我们可以去掉all来去重

🌺子查询

SQL语句中嵌套select语句,称为嵌套查询,又称为子查询

select * from t1 where column1 = ( select column1 from t2 );
子查询的外部语句可以是insert update delete selete的任何一个

🏳️‍🌈根据查询结果不同,分为

查询方式

查询结果

标量子查询

子查询结果为单个值

列子查询

子查询结果为一列

行子查询

子查询结果为一行

表子查询

子查询结果为多行多列

🏳️‍🌈根据子查询位置,分为where之后,from之后,select之后

🎄标量子查询

使用操作符 = > < >= <= 查询结果返回的是单个值这种最简单的方式

🎄列子查询

子查询返回的是一列(可以是多行) 常用操作符:in,not in,any,some,all

操作符

描述

in

在指定的集合范围之内,多选一

not in

不在指定的集合范围之内

any

子查询返回列表中,有任意一个满足即可

some

与any等同,使用some的地方都可以使用any

all

子查询返回列表的所有值都必须满足

🎄行子查询

子查询返回的是一行(可以是多列) 常用操作符:= <> in ,not in

🎄表子查询

子查询返回的是多行多列 常用运算符:in

🥰如果大家有不明白的地方,或者文章有问题,欢迎大家在评论区讨论,指正🥰