【MySQL】学习多表查询和笛卡尔积 - 副本 (2)

32 篇文章 0 订阅
30 篇文章 0 订阅

](https://img-blog.csdnimg.cn/21dd41dce63a4f2da07b9d879ad0120b.png#pic_center)

??个人主页:
??热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法
??个人格言:“没有罗马,那就自己创造罗马~”

#mermaid-svg-N8PeTKG6uLu4bJuM {font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM .error-icon{fill:#552222;}#mermaid-svg-N8PeTKG6uLu4bJuM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-N8PeTKG6uLu4bJuM .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-N8PeTKG6uLu4bJuM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-N8PeTKG6uLu4bJuM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-N8PeTKG6uLu4bJuM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-N8PeTKG6uLu4bJuM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-N8PeTKG6uLu4bJuM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-N8PeTKG6uLu4bJuM .marker.cross{stroke:#333333;}#mermaid-svg-N8PeTKG6uLu4bJuM svg{font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-N8PeTKG6uLu4bJuM .label{font-family:“trebuchet ms”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM .cluster-label text{fill:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM .cluster-label span{color:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM .label text,#mermaid-svg-N8PeTKG6uLu4bJuM span{fill:#333;color:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM .node rect,#mermaid-svg-N8PeTKG6uLu4bJuM .node circle,#mermaid-svg-N8PeTKG6uLu4bJuM .node ellipse,#mermaid-svg-N8PeTKG6uLu4bJuM .node polygon,#mermaid-svg-N8PeTKG6uLu4bJuM .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-N8PeTKG6uLu4bJuM .node .label{text-align:center;}#mermaid-svg-N8PeTKG6uLu4bJuM .node.clickable{cursor:pointer;}#mermaid-svg-N8PeTKG6uLu4bJuM .arrowheadPath{fill:#333333;}#mermaid-svg-N8PeTKG6uLu4bJuM .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-N8PeTKG6uLu4bJuM .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-N8PeTKG6uLu4bJuM .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-N8PeTKG6uLu4bJuM .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-N8PeTKG6uLu4bJuM .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-N8PeTKG6uLu4bJuM .cluster text{fill:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM .cluster span{color:#333;}#mermaid-svg-N8PeTKG6uLu4bJuM div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:“trebuchet ms”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-N8PeTKG6uLu4bJuM :root{–mermaid-font-family:“trebuchet ms”,verdana,arial,sans-serif;}

at one time ‘曾经,一度,同时’

文章目录

多表查询

指的是从多张表中查询数据

  • 首先准备好需要查询的数据表 - dept表 和 emp表

    – 准备数据
    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 emp(
    id int auto_increment comment ‘ID’ primary key ,
    name varchar(50) not null comment ‘姓名’,
    age int comment ‘年龄’,
    job varchar(20) comment ‘职位’,
    salary int comment ‘薪资’,
    entrydate date comment ‘入职时间’,
    managerid int comment ‘直属领导ID’,
    dept_id int comment ‘部门ID’
    )comment ‘员工表’;

    insert into emp (id, name, age, job, salary, entrydate, managerid, dept_id) VALUES
    (1,‘金庸’,66,‘总裁’,2000,‘2000-01-01’,null,5),(2,‘张无忌’,20,‘项目经理’,12500,‘2005-12-05’,1,1),
    (3,‘杨逍’,33,‘开发’,8400,‘2000-11-03’,2,1),(4,‘韦一笑’,48,‘开发’,11000,‘2002-02-05’,2,1),
    (5,‘常遇春’,43,‘开发’,10500,‘2004-09-07’,3,1),(6,‘小昭’,19,‘程序员鼓励师’,6600,‘2004-10-12’,2,1),
    (7,‘灭绝’,60,‘财务总监’,8500,‘2002-09-12’,1,3),(8,‘周芷若’,19,‘会计’,48000,‘2006-06-02’,7,3),
    (9,‘丁敏君’,23,‘出纳’,5250,‘2009-05-13’,7,3),(10,‘赵敏’,20,‘市场部总监’,12500,‘2004-10-12’,1,2),
    (11,‘鹿杖客’,56,‘职员’,3750,‘2006-10-03’,10,2),(12,‘鹤笔翁’,19,‘职员’,3750,‘2007-05-09’,10,2),
    (13,‘方东白’,19,‘职员’,5500,‘2009-02-12’,10,2),(14,‘张三丰’,88,‘销售总监’,14000,‘2004-10-12’,14,4),
    (15,‘俞莲舟’,38,‘销售’,4600,‘2004-10-12’,14,4),(16,‘宋远桥’,40,‘销售’,4600,‘2004-10-12’,14,4),
    (17,‘陈友谅’,42,null,2000,‘2011-10-12’,1,null);

    – 添加外键将两张表进行关联
    alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);

在这里插入图片描述
在这里插入图片描述

-- 多表查询 - 笛卡尔积
select * from emp , dept ;

在这里插入图片描述

  • 笛卡尔积:笛卡尔乘积是指在数学中,两个集合 A 集合和 B集合的所有组合情况。(在多表查询时,需要消除无效的笛卡尔积)—>17*6 = 102 所以我们可以看到102条记录
    在这里插入图片描述

如何消除无效的笛卡尔积?

我们可以通过emp表中的外键和dept的id建立连接,我们只需要在原来基础上加上条件即可

-- 多表查询 - 通过where添加条件,消除无效的笛卡尔积
select * from emp , dept where emp.dept_id = dept.id ;

在这里插入图片描述

通过控制台运行结果我们可以发现我们明明有17个员工而查询到的结果只有16条,这是为什么呢? 这是因为我们的陈友谅这个员工是刚来的员工还没有给他分配部门。??

在这里插入图片描述

多表查询分类

连接查询

内连接:

相当于查询A、B交集部分数据

外连接:

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

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

自连接:

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

字查询

在这里插入图片描述

总结

在这里插入图片描述
](https://img-blog.csdnimg.cn/0ee6c4ec414740b0a0404c5161cdadc7.gif#pic_center)

](https://img-blog.csdnimg.cn/cc002cbd5c414c5393e19c5e0a0dbf20.gif#pic_center#pic_center)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值