当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Minesweeper game - C language
- MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
- 【云原生】DevOps(五):集成Harbor
- Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page
- HCIP Day 10_BGP Route Summary Experiment
- exsl文件预览,word文件预览网页方法
- From scratch, a mirror to the end, a pure system builds a grasscutter (Grasscutter)
- WeChat applet uses cloud functions to update and add cloud database nested array elements
- MySQL to revise the root password
- Doris学习笔记之监控
猜你喜欢
打造基于ILRuntime热更新的组件化开发
BUG消灭者!!实用调试技巧超全整理
The input input box displays the precision of two decimal places
View source and switch mirrors in two ways: npm and nrm
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
递归实现汉诺塔问题
高等数学---第九章二重积分
Blockbuster | foundation for platinum, gold, silver gave nameboards donors
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
HCIP第十天_BGP路由汇总实验
随机推荐
ERP生产作业控制 金蝶
Hand in hand to realize the picture preview plug-in (3)
BUG destroyer!!Practical debugging skills are super comprehensive
Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
【C语言进阶】文件操作(一)
微软 AI 量化投资平台 Qlib 体验
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
Thinking about data governance after Didi fines
Create componentized development based on ILRuntime hot update
Explanation of
.NET-9.乱七八糟的理论笔记(概念,思想)
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
C# 实现PLC的定时器
visual studio 那些提高效率的快捷键,总结(不时更新)
专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
【云原生】DevOps(五):集成Harbor
MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
el-image标签绑定点击事件后没有有用
MySQL数据库必会的增删查改操作(CRUD)
HCIP Day 10_BGP Route Summary Experiment