当前位置:网站首页>MySQL联合查询(多表查询)
MySQL联合查询(多表查询)
2022-08-02 05:19:00 【饿饿好饿】
一、内连接
select *from 表1 [ inner | cross ] join 表2 [ on过滤条件 ] [ where查询条件 ] ;
[ inner | cross ]:
join 内连接关键字(必须要有);
on从语法上可省略,但如果省略它,将查询的是多表的笛卡尔积;
1.select *from 表1 join 表2 [ on过滤条件 ] [ where查询条件 ] ;
2.select *from 表1,表2 [ where查询条件 ] ;
3.select *from 表1 inner join 表2 [ on过滤条件 ] [ where查询条件 ] ;
4.select *from 表1 cross join 表2 [ on过滤条件 ] [ where查询条件 ] ;
(1.2使用较多;多表查询使用1较好)
例1:查询张三的成绩:
1.进行内连接查询(笛卡尔积)
select s.*,st.* from student s join score_table st;
2.去掉无效的数据(on过滤条件)
select s.*,st.* from student s join score_table st on s.id=st.student_id;
3.查询张三的成绩(where 条件)
select s.*,st.* from student s join score_table st on s.id=st.student_id where s.username=‘张三’;
例2:查询每个人成绩、科目名、个人信息:
1.连表查询(三张表)select *from 表1 join 表2 join 表3;
2.过滤笛卡尔积中无意义数据:select *from 表1 join 表2 [ on 条件过滤 ] join 表3[ on 条件过滤 ];
select s.username,s.sn,s.mail,st.score,c.namefrom score_table st join course c on st.course_id=c.id join student s on s.id=st.student_id;
二、外连接:
1.左(外)连接:
select * from 表1 left join 表2 on连接条件 [where条件查询];
表1 查询结果是所有数据,表2查询结果是与表1重合部分的数据
2.右(外)连接
select * from 表1 right join 表2 on连接条件 [where条件查询];
左 / 右连接可以互相实现,只需要将表的顺序调换即可。
联表查询中on和where的区别:
1.on在内连接中可省略,外连接不可省略;
2.on在内连接中执行效果和外连接执行效果不同;
left join…on查询不能过滤掉左表中的数据,而内连接on查询可以过滤掉全局数据。
3.外连接中on和where不同
on筛选笛卡尔积过滤条件,where筛选具体业务
三、自连接(自查询)
select *from 表名 as t1,表名 as t2 where t1.id=t2.id [, …]
例1:查询英语成绩<计算机成绩的数据
(1)根据科目名称查出来科目ID(成绩表中只有科目ID,没有科目名)
(2)自查询
(3)去除笛卡尔积中无意义数据(有意义数据:主键相同;)
(4)设置where条件,让表1只查询英语成绩,表2查询计算机成绩
(5)设置where多条件查询,让英语成绩<计算机成绩
四、子查询(嵌套查询)
select 字段名 from
例:查张三的同班同学
五、合并查询(至少两张表)
union(进行结果集合并,并去重,只保留重复数据中的一条)
union all(不会去掉结果集中的重复行)
例:查询id小于3和名字为“英语”的课程
select * from course where id<3 union select * from course where name=‘英语’;
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- 引领需求 为HR价值正名——“人力资源领先模型HRLM”成功首发
- Deep learning - CNN realizes the recognition of MNIST handwritten digits
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- BGP实验(路由反射器,联邦,路由优化)
- Double for loop case (use js jiujiu printing multiplication table)
- Common functions of pytorch
- 跨桌面端Web容器演进
- 【解决】RESP.app 连接不上redis
- 直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
- What are the ways to improve software testing capabilities?After reading this article, it will take you up a notch
猜你喜欢
selenium + robotframework的运行原理
Not annotated parameter overrides @NonNullApi parameter
Stress testing and performance analysis of node projects
C语言小游戏——扫雷小游戏
Mysql数据库 | 基于Docker搭建Mysql-8.0以上版本主从实例实战
npm、nrm两种方式查看源和切换镜像
Double for loop case (use js jiujiu printing multiplication table)
leetcode solves the linked list merge problem in one step
Redis(十二) - Redis消息队列
Integrate ssm (1)
随机推荐
An advanced method for solving palindromes
为什么4个字节的float要比8个字节的long大呢?
leetcode-338.比特位计数
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
Block elements, inline elements (
elements, span elements)18 years of programmer career, read more than 200 programming books, pick out some essence to share with you
Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟
Shell 脚本不同玩法
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
Double for loop case (use js jiujiu printing multiplication table)
Point Density-Aware Voxels for LiDAR 3D Object Detection Paper Notes
提高软件测试能力的方法有哪些?看完这篇文章让你提升一个档次
Difference and analysis of CPU usage and load
[C language] LeetCode26. Delete duplicates in an ordered array && LeetCode88. Merge two ordered arrays
保证家里和企业中的WIFI安全-附AC与AP组网实验
Meta公司内部项目-RaptorX:将Presto性能提升10倍
C语言小游戏——扫雷小游戏
在腾讯做外包测试的那些日子.....
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
nacos registry