MySQL学习笔记------多表查询

目录

多表关系

一对多

多对多

一对一

多表查询

概述

 分类

内连接(交集)

隐式内连接

显式内连接

​编辑

外连接(from后为左表,join后为右表)

左外连接

右外连接

自连接 

 联合查询(union,union all)

子查询

分类

标量子查询

列子查询 

行子查询 

表子查询


多表关系

概述

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

基本分为三类:一对多,多对多,一对一

一对多

典型案例:部门与员工

实现:在多的一方建立一个外键,指向一的一方的主键

多对多

典型案例:学生与课程的关系

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

#建立学生表
create table student(
    id int auto_increment primary key  comment '主键ID',
    name varchar(10) comment '姓名',
    no varchar(10) comment '学号'
)comment '学生表';
insert into student(id, name, no) VALUES (1,'孔明','1000000000'),(2,'士元','1000000001'),(3,'仲达','1000000003'),(4,',孟德','1000000004');
#建立课程表
create table course(
    id int auto_increment primary key comment '主键ID',
    name varchar(10) comment '课程名称'
)comment '课程表';
insert into course(id, name) VALUES (1,'Java'),(2,'MySQL'),(3,'PHP'),(4,'C++');
#建立学生课程表
create table student_course(
    id int auto_increment primary key comment '主键',
    student_id int not null comment '学生ID',
    course_id int not null comment '课程编码',
    constraint fk_courseid foreign key (course_id)references  course(id),
    constraint fk_student_id foreign key (student_id)references student(id)
)comment '学生课程中间表';
insert into student_course(id, student_id, course_id) VALUES (1,1,1),(2,2,2),(3,3,3),(4,4,4);

添加外键后出现蓝色小钥匙 

一对一

案例:用户与用户详情

关系:一对一,用于单表拆分,将表的基础字段放在一张表中,其他详情字段放到另一张表中,提高操作效率

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

多表查询

概述

建表

create table dept(
    id int auto_increment comment 'ID' primary key ,
    name varchar(50) not null comment '部门名称'
)comment '部门表';
insert into dept(id,name) VALUES (1,'研发部'),(2,'市场部'),(3,'财务部'),(4,'销售部'),(5,'总经办'),(6,'人事部');
create table employee(
    #唯一标识,主键,自动增长为auto_increment
    id int primary key auto_increment comment 'ID',
    #不为空且唯一
    name varchar(10) not null unique comment '姓名',
    #大于零小于120
    age int check ( age>0&&age<120 )comment '年龄',
    job varchar(20) comment '职务',
    salary int comment '薪资',
    entrydate date comment '入职时间',
    dept_id int comment '部门id'
)comment '员工表';
insert into employee(id,name,age,job,salary,entrydate,dept_id)values (1,'关羽',22,'开发',3000,'2018-11-1',1),
                                                                     (2,'刘备',34,'经理',10000,'2011-2-11',2),
                                                                     (3,'张飞',18,'程序员鼓励师',8000,'2020-1-2',4),
                                                                     (4,'曹操',30,'董事长',2000000,'1990-3-4',5),
                                                                     (5,'周瑜',16,'财务总监',10000,'2000-3-5',3);

 查询:

#查询---笛卡尔积
select *from employee,dept;

此时查询,会匹配每个人所有部门匹配一次

修改之后

select *from employee,dept where employee.dept_id=dept.id;

 分类

 例如:AB两表

连接查询

    内连接:相当于查询A、B交集部分数据

    外连接:

        左外连接:查询左表所有数据,以及两张表交集部分数据

        右外连接:查询右表所有数据,以及两边交集部分数据

   自连接:当前表与自身的连接查询,自连接必须使用表别名

子查询

内连接(交集)

内连接分为隐式和显式

隐式内连接

select 字段列表 from 表1,表2 where 条件...;

以上面部门表和员工表为例

#查询员工及其关联部门名称
select employee.name,dept.name from employee,dept where employee.dept_id=dept.id;

显式内连接

select 字段列表 from 表1 [inner] join 表2 on 连接条件...;


#显式
select employee.name,dept.name from employee inner join dept on employee.dept_id=dept.id;

外连接(from后为左表,join后为右表)

左外连接

select 字段列表 from 表1 left [outer] join 表2 on 条件...;

相当于查询表1(左表)的所有数据(包含表交集部分)

#左外连接
select e.*,d.name from employee e left outer join dept d on e.dept_id=d.id;

完全包含左表 

右外连接

select 字段列表 from 表1 right [outer] join 表2 on 条件...;

相当于查询表2(右表)的所有数据(包含表交集部分)

#右外连接
select d.*,e.* from employee e right outer join dept d on e.dept_id=d.id;

自连接 

自连接查询语法(必须取别名)

select 字段列表 from 表A 别名A  jion 表A 别名B on 条件...;(join可省略)

自连接查询,可以是内连接查询,也可以是外连接查询。

#自连接
select a.name,b.name from employee a,employee b where a.manageid=b.id;

 给表添加了上司id

 联合查询(union,union all)

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

语法:

select 字段列表 from 表A  ...

union[all]

select 字段列表 from 表B......;

#联合查询
select * from employee where age>20
union all
select *from employee where salary>20000;

去掉all可去重

注意事项:对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致

子查询

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

语法:

select * from t1 where column1=(select co;lumn1 from t2);

子查询外部的语句可以是insert/update/delect/select的任何一个

分类

根据子查询结果不同可分为

标量子查询(子查询结果为单个值)

列子查询(子查询结果为1列)

行子查询(子查询结果为1行)

表子查询(子查询结果为多行多列)

根据子查询位置分为:where之后、from之后、select之后。

标量子查询

子查询返回的结果是单个值(数字、日期、字符串等),最简单的形式

#标量子查询
select*from employee where dept_id=(select id from dept where name='财务部');
#()内返回值为销售部id

列子查询 

#列子查询
select *from employee where dept_id in (select id from dept where name='财务部'or name='研发部');
#空格内返回一列

行子查询 

#行子查询
select *from employee where (salary,manageid)=(select salary,manageid from employee where name='周瑜');

表子查询

#表子查询
select e.*,d.*from (select*from employee where  entrydate>'2000-01-01') e left outer join dept d on e.dept_id=d.id;

  • 15
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值