七种join的书写规范
在mysql中的两表进行连接时,总共有7种连接情况,具体可见下图

由图的从左到右的顺序
图1.左连接(left join):返回左表中的所有记录和右表中的连接字符字段相等的记录,若右表没有匹配值则补NULL
图2.右连接(right join):返回右表中的所有记录和右表中的连接字符字段相等的记录,若左表没有匹配值则补NULL
图3.内连接(inner join):只有满足条件的巨鹿才会出现在查询结果中,即左表和右表的公共部分
图4.左表中的独自拥有的部分,去除与右表中的重合部分
图5.右表中的独自拥有的部分,去除与左表中的重合部分
图6.左表和右表的重合部分,以及左右表的重和部分(可以看成两表的左连接和右连接的拼接)
图7.左表和右表的独自拥有部分(可以看成是图4和图5的拼接)
实例详解:
创建相关表以及插入数据
create table student(
sid int(11) primary key auto_increment,
sname varchar(20) not null,
class_id int(11) null
);
create table class(
cid int(11) primary key,
cname varchar(20) not null
);
alter table student add foreign key(class_id) references class(cid);
desc student;
insert into class(cid,cname) values
(1,'一年级'),
(2,'二年级'),
(3,'三年级'),
(4,'四年级'),
(5,'五年级'),
(6,'六年级');
set sql_safe_updates = 0;
insert into student(sid,sname,class_id) values
(1,'张三',1),
(2,'李四',4),
(3,'王五',1),
(4,'赵柳',2),
(5,'孙权',6),
(6,'钱升',null),
(7,'刘备',3),
(8,'周往',5);
#内连接
select
student.*,
class.*
from
student
inner join class
on student.class_id = class.cid;

#左连接
select
student.*,
class.*
from
student
left join class
on student.class_id = class.cid;

#右连接
select
student.*,
class.*
from
student
right join class
on student.class_id = class.cid;

#student数据表锁独有的数据
select
student.*,
class.*
from
student
left join class
on student.class_id = class.cid
where class.cid is null;

#class表锁独有的数据
select
student.*,
class.*
from
student
right join class
on student.class_id = class.cid
where student.class_id is null;

select
student.*,
class.*
from
student
left join class
on student.class_id = class.cid
union
select
student.*,
class.*
from
student
right join class
on student.class_id = class.cid;

select
student.*,
class.*
from
student
left join class
on student.class_id = class.cid
where class.cid is null
union
select
student.*,
class.*
from
student
right join class
on student.class_id = class.cid
where student.class_id is null;

七种join的书写规范的更多相关文章
- MySQL逻辑架构、SQL加载执行顺序、七种JOIN模式图解
逻辑架构 存储引擎 查看当前安装的mysql提供的存储引擎 查看当前mysql默认的存储引擎 MyISAM和InnoDB SQL加载执行顺序 sql书写顺序 mysql解析器执行的顺序 考点:m ...
- 【知识库】-数据库_MySQL 的七种 join
掘金作者:haifeisi 文章出处: MySQL 的七种 join Learn [已经过测试校验] 一.内连接 二.左外连接 三.右外连接 四.左连接 五.右连接 六.全连接 七.两张表中都没有出现 ...
- 1. 七种join的sql编写
一.join图 二.sql演示 a.创建演示表及数据 SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ----------------------- ...
- Mysql七种 JOIN 连接
内连接 SELECT <select_list> FROM TableA A INNER JOIN TableB B ON A.Key = B.Key 左外连接 SELECT <se ...
- MySQL的七种join
转载 原文地址 建表 在这里我们先建立两张有外键关联的两张表: CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept` ...
- MySQL 的七种 join
建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...
- MySQL七种join理论
1. 内连接 select * from A inner join B where A.key=B.key; 2. 左连接 select * from A left join B on A.key=B ...
- MYSQL 的七种join
建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...
- ECMA-262规范定义的七种错误类型
第一种:Error 所有错误的基本类型,实际上不会被抛出. 第二种:EvalError 执行eval错误时抛出. 第三种:ReferenceError 对象不存在是抛出. 第四种: ...
随机推荐
- codeforces 8C(非原创)
C. Looking for Order time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- 2021-2-17:Java HashMap 的中 key 的哈希值是如何计算的,为何这么计算?
首先,我们知道 HashMap 的底层实现是开放地址法 + 链地址法的方式来实现. 即数组 + 链表的实现方式,通过计算哈希值,找到数组对应的位置,如果已存在元素,就加到这个位置的链表上.在 Java ...
- TypeScript with React
TypeScript with React # Make a new directory $ mkdir react-typescript # Change to this directory wit ...
- 如何重置电信悦 me 智能网关
如何重置电信悦 me 智能网关 重置电信网关密码 电信悦 me 智能网关密码忘记了怎么办? 首先,得要知道默认终端配置地址和默认终端配置密码. 可以从无线路由器背面标签得知. 如果不知道密码了,可以通 ...
- css dark theme & js theme checker
css dark theme & js theme checker live demo https://codepen.io/xgqfrms/pen/GRprYLm <!DOCTYPE ...
- macOS 屏幕共享, 远程协助
macOS 屏幕共享, 远程协助 Screen Sharing App 隐藏 app bug command + space 搜索 https://macflow.net/p/397.html Tea ...
- Clean Code of JavaScript
Clean Code of JavaScript 代码简洁之道 JavaScript 版 https://github.com/ryanmcdermott/clean-code-javascript ...
- learning free programming resources form top university of the world
learning free programming resources form top university of the world Harvard university https://www. ...
- Dart: puppeteer库
和node的差不多,只有写API不一样 puppeteer 地址 安装依赖 dependencies: puppeteer: ^1.7.1 下载 chrome-win 到 <project_ro ...
- Python算法_三种斐波那契数列算法
斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为"兔子数列&qu ...