SQL执行循序:

手写:

SELECT DISTINCT <query_list> FROM <left_table>
<join type> JOIN <right_table> ON <join_condition>
WHERE <wherer_condition>
GROUP BY <group_by_list>
HAVING <having_condition>
ORDER BY <order_by_list>
LIMIT <limit number>

机读:

FROM <left_table>
ON <join_condition>
<join_type> JOIN <right_table>
WHERE <where_condition>
GROUP BY <group_by_list>
HAVING <having_condition>
SELECT
DISTINCT <query_list>
ORDER BY <order_by_condition>
LIMIT <limit_number>

总结:

理论图谱:

MySQL数据库实例:

1.创建数据库:

mysql> create database db_test;
Query OK, 1 row affected (0.01 sec)

 2.使用数据库:

mysql> use db_test;
Database changed

3.创建表、添加数据:

CREATE TABLE `tb_dept` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '部门主键',
`deptName` varchar(30) DEFAULT NULL COMMENT '部门名称',
`locAdd` varchar(40) DEFAULT NULL COMMENT '楼层',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE `tb_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '员工主键',
`name` varchar(20) DEFAULT NULL COMMENT '员工姓名',
`deptId` int(11) DEFAULT NULL COMMENT '部门外键',
PRIMARY KEY (`id`),
KEY `fk_dept_id` (`deptId`)
#CONSTRAINT `fk_dept_id` FOREIGN KEY (`deptId`) REFERENCES `tb_dept` (`id`) COMMENT '部门外键设置, 已经注释掉。'
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO `tb_dept` VALUES ('', 'RD', '');
INSERT INTO `tb_dept` VALUES ('', 'HR', '');
INSERT INTO `tb_dept` VALUES ('', 'MK', '');
INSERT INTO `tb_dept` VALUES ('', 'MIS', '');
INSERT INTO `tb_dept` VALUES ('', 'FD', ''); INSERT INTO `tb_emp` VALUES ('', '张三', '');
INSERT INTO `tb_emp` VALUES ('', '李四', '');
INSERT INTO `tb_emp` VALUES ('', '王二', '');
INSERT INTO `tb_emp` VALUES ('', '麻子', '');
INSERT INTO `tb_emp` VALUES ('', '小马', '');
INSERT INTO `tb_emp` VALUES ('', '马旭', '');
INSERT INTO `tb_emp` VALUES ('', '小丁', '');
INSERT INTO `tb_emp` VALUES ('', '小西', '');

tb_emp表数据:

mysql> select * from tb_emp;
+----+--------+--------+
| id | name | deptId |
+----+--------+--------+
| 1 | 张三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王二 | 1 |
| 4 | 麻子 | 2 |
| 5 | 小马 | 2 |
| 6 | 马旭 | 3 |
| 7 | 小丁 | 4 |
| 8 | 小西 | 51 |
+----+--------+--------+
8 rows in set (0.01 sec)

tb_dept表数据:

mysql> select * from tb_dept;
+----+----------+--------+
| id | deptName | locAdd |
+----+----------+--------+
| 1 | RD | 11 |
| 2 | HR | 12 |
| 3 | MK | 13 |
| 4 | MIS | 14 |
| 5 | FD | 15 |
+----+----------+--------+
5 rows in set (0.01 sec)

笛卡儿积:

mysql> select * from tb_emp, tb_dept;
+----+--------+--------+----+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+----+--------+--------+----+----------+--------+
| 1 | 张三 | 1 | 1 | RD | 11 |
| 1 | 张三 | 1 | 2 | HR | 12 |
| 1 | 张三 | 1 | 3 | MK | 13 |
| 1 | 张三 | 1 | 4 | MIS | 14 |
| 1 | 张三 | 1 | 5 | FD | 15 |
| 2 | 李四 | 1 | 1 | RD | 11 |
| 2 | 李四 | 1 | 2 | HR | 12 |
| 2 | 李四 | 1 | 3 | MK | 13 |
| 2 | 李四 | 1 | 4 | MIS | 14 |
| 2 | 李四 | 1 | 5 | FD | 15 |
| 3 | 王二 | 1 | 1 | RD | 11 |
| 3 | 王二 | 1 | 2 | HR | 12 |
| 3 | 王二 | 1 | 3 | MK | 13 |
| 3 | 王二 | 1 | 4 | MIS | 14 |
| 3 | 王二 | 1 | 5 | FD | 15 |
| 4 | 麻子 | 2 | 1 | RD | 11 |
| 4 | 麻子 | 2 | 2 | HR | 12 |
| 4 | 麻子 | 2 | 3 | MK | 13 |
| 4 | 麻子 | 2 | 4 | MIS | 14 |
| 4 | 麻子 | 2 | 5 | FD | 15 |
| 5 | 小马 | 2 | 1 | RD | 11 |
| 5 | 小马 | 2 | 2 | HR | 12 |
| 5 | 小马 | 2 | 3 | MK | 13 |
| 5 | 小马 | 2 | 4 | MIS | 14 |
| 5 | 小马 | 2 | 5 | FD | 15 |
| 6 | 马旭 | 3 | 1 | RD | 11 |
| 6 | 马旭 | 3 | 2 | HR | 12 |
| 6 | 马旭 | 3 | 3 | MK | 13 |
| 6 | 马旭 | 3 | 4 | MIS | 14 |
| 6 | 马旭 | 3 | 5 | FD | 15 |
| 7 | 小丁 | 4 | 1 | RD | 11 |
| 7 | 小丁 | 4 | 2 | HR | 12 |
| 7 | 小丁 | 4 | 3 | MK | 13 |
| 7 | 小丁 | 4 | 4 | MIS | 14 |
| 7 | 小丁 | 4 | 5 | FD | 15 |
| 8 | 小西 | 51 | 1 | RD | 11 |
| 8 | 小西 | 51 | 2 | HR | 12 |
| 8 | 小西 | 51 | 3 | MK | 13 |
| 8 | 小西 | 51 | 4 | MIS | 14 |
| 8 | 小西 | 51 | 5 | FD | 15 |
+----+--------+--------+----+----------+--------+
40 rows in set (0.00 sec)

查询tb_emp表和tb_dept中公共的数据:

mysql> select * from tb_emp e
-> inner join tb_dept d on e.deptId = d.id;
+----+--------+--------+----+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+----+--------+--------+----+----------+--------+
| 1 | 张三 | 1 | 1 | RD | 11 |
| 2 | 李四 | 1 | 1 | RD | 11 |
| 3 | 王二 | 1 | 1 | RD | 11 |
| 4 | 麻子 | 2 | 2 | HR | 12 |
| 5 | 小马 | 2 | 2 | HR | 12 |
| 6 | 马旭 | 3 | 3 | MK | 13 |
| 7 | 小丁 | 4 | 4 | MIS | 14 |
+----+--------+--------+----+----------+--------+
7 rows in set (0.00 sec)

查询tb_emp表中全部的数据:

mysql> select * from tb_emp e
-> left join tb_dept d on e.deptId = d.id;
+----+--------+--------+------+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+----+--------+--------+------+----------+--------+
| 1 | 张三 | 1 | 1 | RD | 11 |
| 2 | 李四 | 1 | 1 | RD | 11 |
| 3 | 王二 | 1 | 1 | RD | 11 |
| 4 | 麻子 | 2 | 2 | HR | 12 |
| 5 | 小马 | 2 | 2 | HR | 12 |
| 6 | 马旭 | 3 | 3 | MK | 13 |
| 7 | 小丁 | 4 | 4 | MIS | 14 |
| 8 | 小西 | 51 | NULL | NULL | NULL |    #该条数据为 tb_emp 表中独有的数据, tb_dept 表中的字段用 null 占位
+----+--------+--------+------+----------+--------+
8 rows in set (0.00 sec)

查询tb_dept表中全部的数据:

mysql> select * from tb_emp e
-> right join tb_dept d on e.deptId = d.id;
+------+--------+--------+----+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+------+--------+--------+----+----------+--------+
| 1 | 张三 | 1 | 1 | RD | 11 |
| 2 | 李四 | 1 | 1 | RD | 11 |
| 3 | 王二 | 1 | 1 | RD | 11 |
| 4 | 麻子 | 2 | 2 | HR | 12 |
| 5 | 小马 | 2 | 2 | HR | 12 |
| 6 | 马旭 | 3 | 3 | MK | 13 |
| 7 | 小丁 | 4 | 4 | MIS | 14 |
| NULL | NULL | NULL | 5 | FD | 15 | #该条数据为 tb_dept 表中独有的数据, tb_emp 表中的字段用 null 占位
+------+--------+--------+----+----------+--------+
8 rows in set (0.01 sec)

查询tb_emp表中独有的数据:

mysql> select * from tb_emp e
-> left join tb_dept d on e.deptId = d.id
-> where d.id is null;
+----+--------+--------+------+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+----+--------+--------+------+----------+--------+
| 8 | 小西 | 51 | NULL | NULL | NULL |
+----+--------+--------+------+----------+--------+
1 row in set (0.00 sec)

查询tb_dept表中独有的数据:

mysql> select * from tb_emp e
-> right join tb_dept d on e.deptId = d.id
-> where e.deptId is null;
+------+------+--------+----+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+------+------+--------+----+----------+--------+
| NULL | NULL | NULL | 5 | FD | 15 |
+------+------+--------+----+----------+--------+
1 row in set (0.00 sec)

查询tb_emp和tb_dept表中所有的数据:

mysql> select * from tb_emp e
-> full outer join tb_dept d on e.deptId = d.id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'full outer join tb_dept d on e.deptId = d.id' at line 2

出错原因:mysql 中不支持这种连接方式。

解决办法:左连接数据和右连接数据相加,将公共的部分去重。

mysql> select * from tb_emp e
-> left join tb_dept d on e.deptId = d.id
-> union     #去重
-> select * from tb_emp e
-> right join tb_dept d on e.deptId = d.id;
+------+--------+--------+------+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+------+--------+--------+------+----------+--------+
| 1 | 张三 | 1 | 1 | RD | 11 |
| 2 | 李四 | 1 | 1 | RD | 11 |
| 3 | 王二 | 1 | 1 | RD | 11 |
| 4 | 麻子 | 2 | 2 | HR | 12 |
| 5 | 小马 | 2 | 2 | HR | 12 |
| 6 | 马旭 | 3 | 3 | MK | 13 |
| 7 | 小丁 | 4 | 4 | MIS | 14 |
| 8 | 小西 | 51 | NULL | NULL | NULL |    #该条数据为 tb_emp 表中独有的数据, tb_dept 表中的字段用 null 占位
| NULL | NULL | NULL | 5 | FD | 15 |    #该条数据为 tb_dept 表中独有的数据, tb_emp 表中的字段用 null 占位
+------+--------+--------+------+----------+--------+
9 rows in set (0.00 sec)

查询tb_emp和tb_dept表中独有的数据:

mysql> select * from tb_emp e
-> left join tb_dept d on e.deptId = d.id
-> where d.id is null
-> union
-> select * from tb_emp e
-> right join tb_dept d on e.deptId = d.id
-> where e.deptId is null;
+------+--------+--------+------+----------+--------+
| id | name | deptId | id | deptName | locAdd |
+------+--------+--------+------+----------+--------+
| 8 | 小西 | 51 | NULL | NULL | NULL |
| NULL | NULL | NULL | 5 | FD | 15 |
+------+--------+--------+------+----------+--------+
2 rows in set (0.00 sec)

常见通用的 JOIN 查询的更多相关文章

  1. 4.性能下降原因和常见的Join查询

    性能下降 SQL慢,执行时间长,等待时间长 1.查询语句写的烂 2.索引失效 单值索引失效 和 复合索引失效 3.关联查询太多join(设计缺陷或不得已的需求) 4.服务器调优及各个参数设置(缓冲.线 ...

  2. Mysql 拼接字段查询语句和join查询拼接和时间查询

    个人平时记录的,有点乱 1.修改时间字段,如果时间字段的类型是date或者是datetime类型的 update 表名 set 时间字段 = DATE_FORMAT(NOW(),'%Y-%m-%d % ...

  3. mysql join 查询图

    mysql join 查询,特别是对查两个表之间的差集,可以用table字段=null来做. 注意千万不是join on XX!=XX  ,这样出来的结果是错误的.

  4. Map/Reduce中Join查询实现

    张表,分别较data.txt和info.txt,字段之间以/t划分. data.txt内容如下: 201001    1003    abc 201002    1005    def 201003  ...

  5. SQL2-子查询、join查询

    SQL常用高级查询包括:Join查询.子查询. 子查询: USE flowershopdb --子查询:在一个select语句使用另一个select 语句作为条件或数据来源. --查询块:一个sele ...

  6. SQL-29 使用join查询方式找出没有分类的电影id以及名称

    题目描述 film表 字段 说明 film_id 电影id title 电影名称 description 电影描述信息 CREATE TABLE IF NOT EXISTS film ( film_i ...

  7. hive的join查询

    hive的join查询 语法 join_table: table_reference [INNER] JOIN table_factor [join_condition] | table_refere ...

  8. 09—mybatis注解配置join查询

    今天来聊mybatis的join查询,怎么说呢,有的时候,join查询确实能提升查询效率,今天举个left join的例子,来看看mybatis的join查询. 就不写的很细了,把主要代码贴出来了. ...

  9. 一条SQL完成跨数据库实例Join查询

    背景 随着业务复杂程度的提高.数据规模的增长,越来越多的公司选择对其在线业务数据库进行垂直或水平拆分,甚至选择不同的数据库类型以满足其业务需求.原本在同一数据库实例里就能实现的SQL查询,现在需要跨多 ...

随机推荐

  1. scrapy编写爬虫的时候出现缺少win32api

    环境:python3.6 工具:pycharm2017.3 scrapy fetch http://www.baidu.com ModuleNotFoundError: No module named ...

  2. python2.7入门---面向对象

        Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.所以,这篇文章我们来记录下Python的面向对象编程.如果你以前没有接触过面向对象的编 ...

  3. python基础之try异常处理、socket套接字基础part1

    异常处理 错误 程序里的错误一般分为两种: 1.语法错误,这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正 2.逻辑错误,人为造成的错误,如数据类型错误.调用方法错误等,这些解 ...

  4. WPF中的命令与命令绑定(一)

    原文:WPF中的命令与命令绑定(一)   WPF中的命令与命令绑定(一)                                           周银辉说到用户输入,可能我们更多地会联想到 ...

  5. PHP通过copy()函数来复制一个文件

    PHP通过copy()函数来复制一个文件.用法如下: bool copy(string $source, string $dest) 其中$source是源文件的路径,$dest是目的文件的路径.函数 ...

  6. python 快速搭建文件服务器

    以http方式共享当前文件夹的文件 可实现跨平台文件传输 只需要一句话搞定 #python -m SimpleHTTPServer 8001 浏览器中输入 http://ip:8001  显示所有文件 ...

  7. linux命令大全(转载)

    在搭建openstack时遇到问题,导致上网查询相关信息.找到一篇不错的文章,希望对大家有用.下附地址: http://blog.csdn.net/junbujianwpl/article/detai ...

  8. deepin linux 安装/启动jeakins报错:处理

    ERROR: No Java executable found in current PATH: /bin:/usr/bin:/sbin:/usr/sbin 安装报错: 1.如还未安装java,则安装 ...

  9. Ubuntu18.04 + CUDA9.0 + cuDNN7.3 + Tensorflow-gpu-1.12 + Jupyter Notebook深度学习环境配置

    目录 一.Ubuntu18.04 LTS系统的安装 1. 安装文件下载 2. 制作U盘安装镜像文件 3. 开始安装 二.设置软件源的国内镜像 1. 设置方法 2.关于ubuntu镜像的小知识 三.Nv ...

  10. shell及Python爬虫实例展示

    1.shell爬虫实例: [root@db01 ~]# vim pa.sh #!/bin/bash www_link=http://www.cnblogs.com/clsn/default.html? ...