当前位置:网站首页>Multiple table query of sql statement
Multiple table query of sql statement
2022-07-31 04:53: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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- 【ORACLE Explain 详解】
- The 15th day of the special assault version of the sword offer
- Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays
- unity2d game
- three.js make 3D photo album
- 关于出现大量close_wait状态的理解
- 矩池云快速安装torch-sparse、torch-geometric等包
- MySQL数据库增删改查(基础操作命令详解)
猜你喜欢

CentOS7 —— yum安装mysql

MySQL database backup

开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源

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

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

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution

Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays

Vue项目通过node连接MySQL数据库并实现增删改查操作

Unity资源管理系列:Unity 框架如何做好资源管理
![[Linear Neural Network] softmax regression](/img/98/ffdff5e96ad0d6ac0142af7ca11e9a.png)
[Linear Neural Network] softmax regression
随机推荐
SQL语句中对时间字段进行区间查询
打造基于ILRuntime热更新的组件化开发
WeChat applet uses cloud functions to update and add cloud database nested array elements
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
三道leetcode上的oj题
MySQL database backup
MySQL优化:从十几秒优化到三百毫秒
C Implementation of Simple Network File Copy
参考代码系列_1.各种语言的Hello World
ERP生产作业控制 金蝶
SQL行列转换
30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully
sql statement - how to query data in another table based on the data in one table
MySQL database installation (detailed)
ES 源码 API调用链路源码分析
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
WPF WPF 】 【 the depth resolution of the template
sql语句之多表查询
VScode+ESP32 quickly install ESP-IDF plugin