mysql加载顺序

手写顺序

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

机读顺序

 1. FROM <left_table>
2. ON <join_condition>
3. <join_type> JOIN <right_table>
4. WHERE <where_condition>
5. GROUP BY <group_by_list>
6. HAVING <having_condition>
7. SELECT
8. DISTINCT <select list>
9. ORDER BY <order_by_condition>
10. LIMIT <limit_number>

sql语句的执行顺序可以用这张鱼骨图来表示

join连表

mysql中的连表基本可以分为以下几种。

接下来对这几种写出相应的sql语句。

首先是创建相应的表来进行实践。

create table if not exists tbl_dept(
id int not null auto_increment primary key,
deptName varchar(30),
locAdd varchar(40)
); create table if not exists tbl_emp(
id int auto_increment primary key,
name varchar(20),
depid int
); insert into tbl_dept(deptName, locAdd) values('RD', 11);
insert into tbl_dept(deptName, locAdd) values('HR', 12);
insert into tbl_dept(deptName, locAdd) values('MK', 13);
insert into tbl_dept(deptName, locAdd) values('MIS', 14);
insert into tbl_dept(deptName, locAdd) values('FD', 15); insert into tbl_emp(name, depid) values('z3', 1);
insert into tbl_emp(name, depid) values('z4', 1);
insert into tbl_emp(name, depid) values('z5', 1);
insert into tbl_emp(name, depid) values('w5', 2);
insert into tbl_emp(name, depid) values('w6', 2);
insert into tbl_emp(name, depid) values('s7', 3);
insert into tbl_emp(name, depid) values('s8', 4);
insert into tbl_emp(name, depid) values('s9', 51);

内连接(等值连接)

mysql> select * from tbl_emp as e inner join tbl_dept as d on e.depid=d.id;
+----+------+-------+----+----------+--------+
| id | name | depid | id | deptName | locAdd |
+----+------+-------+----+----------+--------+
| 1 | z3 | 1 | 1 | RD | 11 |
| 2 | z4 | 1 | 1 | RD | 11 |
| 3 | z5 | 1 | 1 | RD | 11 |
| 4 | w5 | 2 | 2 | HR | 12 |
| 5 | w6 | 2 | 2 | HR | 12 |
| 6 | s7 | 3 | 3 | MK | 13 |
| 7 | s8 | 4 | 4 | MIS | 14 |
+----+------+-------+----+----------+--------+
7 rows in set (0.01 sec)

左连接(连接左表的全部,右表缺失的字段以null补齐)

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id;
+----+------+-------+------+----------+--------+
| id | name | depid | id | deptName | locAdd |
+----+------+-------+------+----------+--------+
| 1 | z3 | 1 | 1 | RD | 11 |
| 2 | z4 | 1 | 1 | RD | 11 |
| 3 | z5 | 1 | 1 | RD | 11 |
| 4 | w5 | 2 | 2 | HR | 12 |
| 5 | w6 | 2 | 2 | HR | 12 |
| 6 | s7 | 3 | 3 | MK | 13 |
| 7 | s8 | 4 | 4 | MIS | 14 |
| 8 | s9 | 51 | NULL | NULL | NULL |
+----+------+-------+------+----------+--------+
8 rows in set (0.03 sec)

右连接(连接右表的全部,左表缺失的字段以null补齐)

mysql> select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id;
+------+------+-------+----+----------+--------+
| id | name | depid | id | deptName | locAdd |
+------+------+-------+----+----------+--------+
| 1 | z3 | 1 | 1 | RD | 11 |
| 2 | z4 | 1 | 1 | RD | 11 |
| 3 | z5 | 1 | 1 | RD | 11 |
| 4 | w5 | 2 | 2 | HR | 12 |
| 5 | w6 | 2 | 2 | HR | 12 |
| 6 | s7 | 3 | 3 | MK | 13 |
| 7 | s8 | 4 | 4 | MIS | 14 |
| NULL | NULL | NULL | 5 | FD | 15 |
+------+------+-------+----+----------+--------+
8 rows in set (0.03 sec)

左独占连接

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id where d.id is null;
+----+------+-------+------+----------+--------+
| id | name | depid | id | deptName | locAdd |
+----+------+-------+------+----------+--------+
| 8 | s9 | 51 | NULL | NULL | NULL |
+----+------+-------+------+----------+--------+
1 row in set (0.04 sec)

右独占连接

mysql> select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id where e.id is null;
+------+------+-------+----+----------+--------+
| id | name | depid | id | deptName | locAdd |
+------+------+-------+----+----------+--------+
| NULL | NULL | NULL | 5 | FD | 15 |
+------+------+-------+----+----------+--------+
1 row in set (0.04 sec)

全连接

由于 mysql中不支持全连接,所以需要使用union来进行模拟。

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id
union
select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id;
+------+------+-------+------+----------+--------+
| id | name | depid | id | deptName | locAdd |
+------+------+-------+------+----------+--------+
| 1 | z3 | 1 | 1 | RD | 11 |
| 2 | z4 | 1 | 1 | RD | 11 |
| 3 | z5 | 1 | 1 | RD | 11 |
| 4 | w5 | 2 | 2 | HR | 12 |
| 5 | w6 | 2 | 2 | HR | 12 |
| 6 | s7 | 3 | 3 | MK | 13 |
| 7 | s8 | 4 | 4 | MIS | 14 |
| 8 | s9 | 51 | NULL | NULL | NULL |
| NULL | NULL | NULL | 5 | FD | 15 |
+------+------+-------+------+----------+--------+
9 rows in set (0.04 sec)

左独占连接+右独占连接

同理使用union连接来进行模拟

mysql> select * from tbl_emp as e left join tbl_dept as d on e.depid=d.id where d.id is null
union
select * from tbl_emp as e right join tbl_dept as d on e.depid=d.id where e.id is null;
+------+------+-------+------+----------+--------+
| id | name | depid | id | deptName | locAdd |
+------+------+-------+------+----------+--------+
| 8 | s9 | 51 | NULL | NULL | NULL |
| NULL | NULL | NULL | 5 | FD | 15 |
+------+------+-------+------+----------+--------+
2 rows in set (0.04 sec)

mysql执行顺序与join连接的更多相关文章

  1. mysql执行顺序及左连接和右连接

    SELECT语句执行顺序 SELECT语句中子句的执行顺序与SELECT语句中子句的输入顺序是不一样的,所以并不是从SELECT子句开始执行的,而是按照下面的顺序执行: 开始->FROM子句-& ...

  2. SQL子句执行顺序和Join的一点总结

    SQL子句执行顺序和Join的一点总结 FROM ON JOIN WHERE GROUP BY WITH CUBE or WITH ROLLUP HAVING SELECT DISTINCT ORDE ...

  3. mysql执行顺序

    SELECT语句执行顺序 SELECT语句中子句的执行顺序与SELECT语句中子句的输入顺序是不一样的,所以并不是从SELECT子句开始执行的,而是按照下面的顺序执行: 开始->FROM子句-& ...

  4. 关于sql和MySQL的语句执行顺序(必看!!!)

    今天遇到一个问题就是mysql中insert into 和update以及delete语句中能使用as别名吗?目前还在查看,但是在查阅资料时发现了一些有益的知识,给大家分享一下,就是关于sql以及My ...

  5. 关于sql和MySQL的语句执行顺序

    sql和mysql执行顺序,发现内部机制是一样的.最大区别是在别名的引用上. 一.sql执行顺序 (1) from (3) join (2) on (4) where (5) group by(开始使 ...

  6. mysql 中sql的执行顺序

    文章转自 https://www.cnblogs.com/annsshadow/p/5037667.html https://www.cnblogs.com/yyjie/p/7788428.html ...

  7. Mysql 语句执行顺序

    1.这样一个问题,作为一个开发人员需要掌握数据库的哪些东西?  在开发中涉及到数据库,基本上只用到了sql语句,如何写sql以及对其进行优化就比较重要,那些mysql的厚本书籍针对的是DBA,我们只需 ...

  8. mysql sql的执行顺序

    转:http://blog.csdn.net/u014044812/article/details/51004754 关于sql和MySQL的语句执行顺序(必看!!!) 原创 2016年03月29日 ...

  9. (转)关于sql和MySQL的语句执行顺序(必看!!!)

    原文:https://blog.csdn.net/u014044812/article/details/51004754 https://blog.csdn.net/j080624/article/d ...

随机推荐

  1. Fluent导出残差总结

    在使用Fluent进行求解的时候,有时候我们需要将求解的残差提取出来,进行后续的处理,我们可以采用下面的方法将Fluent求解残差输出.下面我们用一个简单的二维算例来说明(算例来源于:https:// ...

  2. [1-2] Dependence-Aware Service Function Chain Design and Mapping

    文献名称:Dependence-Aware Service Function Chain Design and Mapping 文献类型(期刊.硕论.博论):会议:Globecom 发表年份:2017 ...

  3. linux , nginx: 封禁IP的办法【转】

    今天,我们的一台服务器出了问题: 被若干IP地址访问某个接口,该接口会发送短信. 所以,我们可以做两件事: 1. nginx的层面封IP .  2  linux server的层面封IP 先看ngin ...

  4. python PIL 图像处理

    python PIL 图像处理 This blog is from: https://www.jianshu.com/p/e8d058767dfa Image读出来的是PIL的类型,而skimage. ...

  5. DVT JetBrains License Server(JetBrains授权服务器)2018 v1.1 最新版 含32位/64位

    DVT JetBrains License Server是JetBrains系列软件授权服务器,支持2017版本得jetbrains pycharm,JetBrainswebstorm,JetBrai ...

  6. SELECT DISTINCT ON expressions must match initial ORDER BY expressions

    开发说pg中执行sql报错,发来消息让帮看看: SELECT DISTINCT ON expressions must match initial ORDER BY expressions 详细语句如 ...

  7. java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect

    添加这个依赖 <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystr ...

  8. fatal error: sys/videoio.h: No such file or directory

    Determining if the include file sys/videoio.h exists failed with the following output:Change Dir: /h ...

  9. SDN实验---Mininet实验(模拟多数据中心带宽实验)

    补充:NameError: name 'buffer' is not defined >>> import sys >>> ,): ... buffer = mem ...

  10. 深度相机Astra Pro测试教程

    最近在微信群内,很多群友在群友的推荐下,购买了Astra pro的深度相机,价格地道,物超所值!群友反馈积极,所以这里出一波简单的教程.   以下内容知识抛砖引玉,主要讲解windows下和Ubunt ...