当前位置:网站首页>sql语句之多表查询
sql语句之多表查询
2022-07-31 04:47:00 【jiey0407】
文章目录
一、笛卡尔积(交叉连接) ---->cross join
1、概念
笛卡尔积又称为“交叉连接”,将两个表中所有数据行。返回结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询条件的数据行数。
(相当于合并两张表单为一张,表单中所有字段都保存展示)
例如,A={a,b},B={0,1,2} 则
AxB={<a,o>,<a,1>,<a,2>,<b,0>,<b,1>,<b,2>,}
BxA={<0,a>,<0,b>,<1,a>,<1,b>,<2,a>,<2,b>}
2. 语法
在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用’,’
SELECT * FROM table1 CROSS JOIN table2;
SELECT * FROM table1 JOIN table2;
SELECT * FROM table1 , table2;
3.示例
示例1、合并两个表内容(纯粹连接查询)
select * from employees,dept_emp;


二、内连接---->inner join
1、概念
在MySQL中把INNER JOIN叫做等值连接,即需要指定等值连接条件INNER JOIN被划分在一起。
(1)、必须有等值条件;
(2)、内连接可能会丢失信息;
(内连接是从结果表中删除与其他被连接表中没有匹配行的所有行)
(3)、重点:内连接,只查匹配行。
2. 语法
第一种:只使用where
SELECT * FROM table1 a,table2 b where a.id=b.id ;
第二种:join…on
SELECT * FROM table1 a JOIN table2 b on a.id=b.id;
第三种:inner join … on
SELECT * FROM table1 a inner join table2 b on a.id=b.id;
3.示例
select * from salaries a,dept_manager b where a.emp_no=b.emp_no;
select * from salaries a join dept_manager b on a.emp_no=b.emp_no;

三、外链接---->outer join
1、概念
分为左外连接( LEFT OUTER JON )和右连接(RIGHT OUTER JOIN),
即除了返回符合连接条件的结果之外,
还要返回左表(左连接)或者右表(右连接)中不符合连接条件的结果,相对应的使用NULL对应。
1、左外连接 left inner join
左边主表,左所有数据都显示,右边显示相关联 ,没有数据显示null
2、右外连接 right inner join
右边主表,右所有数据都显示,左边显示相关联 ,没有数据显示null
2. 语法
SELECT * FROM table1 a left join table2 b on a.id=b.id ;
SELECT * FROM table1 a right join table2 b on a.id=b.id;
3.示例
(1)两张表以左表为准
select * from salaries a left join dept_manager b on a.emp_no=b.emp_no;

(2)三张表
A left join B on A.id=B.id left join C on B.code=C.code
SELECT last_name, first_name, dept_name
FROM employees a LEFT JOIN dept_emp b ON a.emp_no=b.emp_no
LEFT JOIN departments c ON b.dept_no=c.dept_no;

四、联合连接---->union
1、概念
两个sql语句,中间用union连接
union all 分别查看&不去重
2.示例
select device_id, gender, age, gpa
from user_profile
where university='山东大学'
union all
select device_id, gender, age, gpa
from user_profile
where gender='male'

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- VScode+ESP32快速安装ESP-IDF插件
- PCL calculates the point cloud coordinate maximum and its index
- Can't load /home/Iot/.rnd into RNG
- Unity手机游戏性能优化系列:针对CPU端的性能调优
- ABC D - Distinct Trio(k元组的个数
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- 产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
- MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
- Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
- qlib架构
猜你喜欢
![[Linear Neural Network] softmax regression](/img/98/ffdff5e96ad0d6ac0142af7ca11e9a.png)
[Linear Neural Network] softmax regression

STM32HAL library modifies Hal_Delay to us-level delay

ERROR 1819 (HY000) Your password does not satisfy the current policy requirements

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

MySQL database must add, delete, search and modify operations (CRUD)
![[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters](/img/98/282d8d192f701ad33de48aeeb038b4.png)
[R language] [3] apply, tapply, lapply, sapply, mapply and par function related parameters

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

XSS shooting range (3) prompt to win

Win10 CUDA CUDNN installation configuration (torch paddlepaddle)

Unity Fighter
随机推荐
C language confession code?
Recursive implementation of the Tower of Hanoi problem
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
三道leetcode上的oj题
Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]
[py script] batch binarization processing images
MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
PCL 计算点云坐标最值及其索引
STM32HAL库修改Hal_Delay为us级延时
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
MySQL database must add, delete, search and modify operations (CRUD)
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
qlib自动化quant
Explanation of
已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
简易网络文件拷贝的C实现
.NET-6.WinForm2.NanUI学习和总结
PWN ROP
行业落地呈现新进展 | 2022开放原子全球开源峰会OpenAtom OpenHarmony分论坛圆满召开
Basic knowledge of mysql (2)