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. js处理事件冒泡(兼容写法)

    event = event || window.event; if (event.stopPropagation) { event.stopPropagation(); } else { event. ...

  2. 实现mysql的读写分离(mysql-proxy)____1(mysql的主从复制,基于gtid的主从复制,半同步复制,组复制)

    主从复制原理: 从库生成两个线程,一个I/O线程,一个SQL线程: i/o线程去请求主库 的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中:主库会生成一个 log ...

  3. GIT-本地仓库

    用户配置 git config --global user.name "name" git config --global user.email "123@qq.com& ...

  4. Python全栈工程师(Python3 所有基础内容 0-0)

    转发:https://www.cnblogs.com/ParisGabriel/p/9388030.html statements  语句print   输出quit()  退出exit() 退出ct ...

  5. php – 通过curl从url获取JSON数据

    我试图通过curl连接从URL获取JSON数据.当我打开链接时:它显示{“version”:“N / A”,“success”:true,“status”:true}.现在,我希望获得以上内容. 到目 ...

  6. UDP丢包原因总结

    丢包检查方法 给每个UDP包编号,对比收发端的接收到的包.对于UDP协议层上的包,例如RTP包,可以从RTP包中读出包的序列号进行判断. 抓包.发送端和接收端分别抓包.linux下可以使用tcpdum ...

  7. Comparable接口的使用

    功能: Comparable接口可用于对象的排序或者对象的分组 介绍: Comparable接口强行对实现它的类的每个实例进行自然排序,该接口的唯一方法compareTo方法被称为自然比较方法 方法: ...

  8. cannot connect to daemon at tcp:5037: cannot connect to 127.0.0.1:5037: 由于目标计算机积极拒绝,无法连接。 (10061)

    原因是:5037端口被占用 解决方法:1.查找5037被谁占用   netstat -ano | findstr "3037" 2.查看对应的是哪个任务  tasklist | f ...

  9. 多线程高效合作之master-warker模式

    对于高并发的任务,有些任务是相互独立的,任务与任务之间没有依赖关系,因此可以采用 master - worker 模式. master 用于接受任务和分发任务给 worker,并将 worker 返回 ...

  10. 使用leaflet绘制geoJson中国边界

    绘制中国边界 代码如下: function drawChina() { //设置样式 var myStyle = { "color": "#00f", &quo ...