转载  一张图看懂 SQL 的各种 join 用法

一、JOIN 使用介绍

下面例子使用的数据表如下:

-- ----------------------------
-- Table structure for dancing_hobby
-- ----------------------------
DROP TABLE IF EXISTS `dancing_hobby`;
CREATE TABLE `dancing_hobby` (
`id` int(11) NOT NULL COMMENT '主键',
`stu_num` int(11) NULL DEFAULT NULL COMMENT '学号',
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`age` varchar(3) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '年龄',
`sex` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '男生1 女生2',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ----------------------------
-- Records of dancing_hobby
-- ----------------------------
INSERT INTO `dancing_hobby` VALUES (1, 1001, '小红', '', '');
INSERT INTO `dancing_hobby` VALUES (2, 1002, '小红', '', '');
INSERT INTO `dancing_hobby` VALUES (3, 1003, '小黑', '', '');
INSERT INTO `dancing_hobby` VALUES (4, 2001, '大绿', '', '');
INSERT INTO `dancing_hobby` VALUES (5, 1005, '小紫', '', '');
INSERT INTO `dancing_hobby` VALUES (6, 2002, '大蓝', '', '');
INSERT INTO `dancing_hobby` VALUES (7, 1007, '小黄', '', '');
INSERT INTO `dancing_hobby` VALUES (8, 2003, '大白', '', '');
INSERT INTO `dancing_hobby` VALUES (9, 1009, '小橙', '', '');
INSERT INTO `dancing_hobby` VALUES (10, 2004, '大青', '', ''); -- ----------------------------
-- Table structure for sing_hobby
-- ----------------------------
DROP TABLE IF EXISTS `sing_hobby`;
CREATE TABLE `sing_hobby` (
`id` int(11) NOT NULL COMMENT '主键',
`stu_num` int(11) NULL DEFAULT NULL COMMENT '学号',
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '姓名',
`age` varchar(3) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '年龄',
`sex` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '男生1 女生2',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ----------------------------
-- Records of sing_hobby
-- ----------------------------
INSERT INTO `sing_hobby` VALUES (1, 1001, '小红', '', '');
INSERT INTO `sing_hobby` VALUES (2, 1002, '小红', '', '');
INSERT INTO `sing_hobby` VALUES (3, 1003, '小黑', '', '');
INSERT INTO `sing_hobby` VALUES (4, 1004, '小绿', '', '');
INSERT INTO `sing_hobby` VALUES (5, 1005, '小紫', '', '');
INSERT INTO `sing_hobby` VALUES (6, 1006, '小蓝', '', '');
INSERT INTO `sing_hobby` VALUES (7, 1007, '小黄', '', '');
INSERT INTO `sing_hobby` VALUES (8, 1008, '小白', '', '');
INSERT INTO `sing_hobby` VALUES (9, 1009, '小橙', '', '');
INSERT INTO `sing_hobby` VALUES (10, 1010, '小青', '', '');

1.INNER JOIN(内连接)

INNER JOIN 一般被译作内连接。内连接查询能将左表(表 A)和右表(表 B)中能关联起来的数据连接后返回。

SELECT <select_list>
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key

例子:

-- 内连接
SELECT
sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM
sing_hobby sing
inner JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num

2.LEFT JOIN(左连接)

LEFT JOIN 一般被译作左连接,也写作 LEFT OUTER JOIN。左连接查询会返回左表(表 A)中所有记录,不管右表(表 B)中有没有关联的数据。在右表中找到的关联数据列也会被一起返回。

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key

例子:

-- 左连接
SELECT
sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM
sing_hobby sing
LEFT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num

3.RIGHT JOIN(右连接)

RIGHT JOIN 一般被译作右连接,也写作 RIGHT OUTER JOIN。右连接查询会返回右表(表 B)中所有记录,不管左表(表 A)中有没有关联的数据。在左表中找到的关联数据列也会被一起返回。

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

例子:

-- 右连接
SELECT
sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM
sing_hobby sing
RIGHT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num

4.FULL OUTER JOIN(外连接)

FULL OUTER JOIN 一般被译作外连接、全连接,实际查询语句中可以写作 FULL OUTER JOIN 或 FULL JOIN。外连接查询能返回左右表里的所有记录,其中左右表里能关联起来的记录被连接后返回。

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key

注:mysql不支持外连接,可以使用 UNION ALL 代替,但要注意,如果要查询的两个表不是全部字段,那查询两个表的字段个数要相同,如下例子。

例子:

-- 外连接
SELECT sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM sing_hobby sing
LEFT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
UNION ALL
SELECT sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM sing_hobby sing
RIGHT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
WHERE sing.stu_num IS NULL -- 外连接
SELECT
sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM
sing_hobby sing
FULL JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num

5.LEFT JOIN EXCLUDING INNER JOIN(左连接-内连接)

返回左表有,右表没有的关联数据记录集。

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

例子:

-- 左内连接
SELECT
sing.`name` AS sing_name,
sing.sex sing_sex,
dancing.`name` dancing_name,
dancing.sex dancing_sex
FROM
sing_hobby sing
LEFT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
WHERE
dancing.stu_num IS NULL

6.RIGHT JOIN EXCLUDING INNER JOIN(右连接-内连接)

返回右表有,左表没有的关联数据记录集。

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL

例子:

-- 右内连接
SELECT
sing.`name` AS sing_name,
sing.sex sing_sex,
dancing.`name` dancing_name,
dancing.sex dancing_sex
FROM
sing_hobby sing
RIGHT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
WHERE
sing.stu_num IS NULL

7.OUTER JOIN EXCLUDING INNER JOIN(外连接-内连接)

返回右表、左表没有的关联数据记录集。

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL OR B.Key IS NULL

例子:

-- 排除内连接的外连接
SELECT sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM sing_hobby sing
LEFT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
WHERE sing.stu_num IS NULL OR dancing.stu_num IS NULL
UNION ALL
SELECT sing.`name` as sing_name,sing.sex sing_sex,dancing.`name` dancing_name,dancing.sex dancing_sex
FROM sing_hobby sing
RIGHT JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
WHERE sing.stu_num IS NULL OR dancing.stu_num IS NULL -- 排除内连接的外连接
SELECT
sing.`name` AS sing_name,
sing.sex sing_sex,
dancing.`name` dancing_name,
dancing.sex dancing_sex
FROM
sing_hobby sing
FULL JOIN dancing_hobby dancing ON sing.stu_num = dancing.stu_num
WHERE
sing.stu_num IS NULL OR dancing.stu_num IS NULL

mysql学习之join用法的更多相关文章

  1. mysql中的JOIN用法总结

    join是mysql中一个基础的关键词,一般在多表连接查询中使用,这里做一下总结 1.JOIN的语法格式 table_references: table_reference [, table_refe ...

  2. mysql学习之join从句

    一.join从句共有5种类型 内连接(inner join) 全外连接(full outer join) 左外连接(left outer join) 右外连接(right outer join) 交叉 ...

  3. MySQL学习 EXISTS的用法 转载

    比如在Northwind数据库中有一个查询为 SELECT c.CustomerId,CompanyName FROM Customers c WHERE EXISTS( SELECT OrderID ...

  4. 一张图概括mysql的各种join用法

  5. MySQL基础之STRAIGHT JOIN用法简介

    MySQL基础之STRAIGHT JOIN用法简介 引用mysql官方手册的说法: STRAIGHT_JOIN is similar to JOIN, except that the left tab ...

  6. mysql中left join right join inner join用法分析

    mysql数据库中的关联查询,基本都会用到left join,right join,inner join等查询方式,今天来说说这三种用法的区别 1.创建表test1,test2,插入测试数据 #创建表 ...

  7. MySQL应用之CROSS JOIN用法简介教程

    目录 2. cross join用法 @ 本博客翻译自两篇博客的: http://www.mysqltutorial.org/mysql-cross-join/ https://www.w3resou ...

  8. Hive 文件格式 & Hive操作(外部表、内部表、区、桶、视图、索引、join用法、内置操作符与函数、复合类型、用户自定义函数UDF、查询优化和权限控制)

    本博文的主要内容如下: Hive文件存储格式 Hive 操作之表操作:创建外.内部表 Hive操作之表操作:表查询 Hive操作之表操作:数据加载 Hive操作之表操作:插入单表.插入多表 Hive语 ...

  9. 一千行MySQL学习笔记 (转)

    出处:  一千行MySQL学习笔记 /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权 ...

随机推荐

  1. Maven运行时找不到xml文件和properties文件的问题解决

    使用Maven构建的项目,包下面的xml文件和properties属性文件默认在运行tomcat插件是不会生成文件到target里面的,需要自己配置 一.第一种解决方法也是最常用的解决方法 在pom. ...

  2. 鼠标拖动改变DIV等网页元素的大小的最佳实践

    1.初次实现 1.1 html代码 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" la ...

  3. 网站SEO优化

    网站的优化应该迎合搜索引擎,这样才能得到事半功倍的效果! 一.站内优化 1.做好HTML头标签 标题(title):标题是网页优化中相当有分量,一般网页title主要包含一些关键词.网站名称等.关键词 ...

  4. CSS之inline和inline-block

    inline-block 控制台-代码: PS:inline-block是让元素以内联形式存在,也就是不是块级,但是表现起来(又具有块级元素的高度)--也就是可以调高度(margin或者padding ...

  5. cf1043F. Make It One(dp 容斥原理)

    题意 题目链接 给出\(n\)个数,问最少选几个数,使他们的\(gcd = 1\) Sol 好神仙啊qwq. 首先,如果答案存在,那么最多为\(7\)(因为前\(7\)个质数乘起来\(>= 3e ...

  6. UNIX/Linux系统管理技术手册(2)----bash脚本编程

    1. 一个简单的例子: $ vim readname.sh #file:readname.sh#!/bin/bash echo -n "Enter your name: " rea ...

  7. 项目经验:GIS<MapWinGIS>建模第一天

    随便记录下项目的进展情况: GIS平台系统,实现整个供水系统的协调与统一.系统以管网为基础依据,建立可实现供水管网规划设计.输配管理.图档管理.抢修辅助决策及综合查询.统计等功能. 本文档的预期读者是 ...

  8. 初学js的穷举思想

    初学者,最关机键的,就是掌握for的穷举思想. 穷举:穷尽.完全.全部. 具体方法: 外层:用for循环一一列举所有可能性 内层:用if语句进行判断,如果满足条件就输出,不满足的跳出进行下次循环. & ...

  9. Fastdfs 部署干货

    tracker server and client:192.168.1.42 storage server:192.168.1.46 storage server:192.168.1.53 安装: 安 ...

  10. 并发包CallableAndFuture

    /** * * @描述: Callable Future * * 程序运行一个线程,线程运行结束后,我们可以获取另一个线程的结果 * * @作者: Wnj . * @创建时间: 2017年5月16日 ...