MySQL Crash Course #08# Chapter 16. Using Different Join Types
记文档还是相当重要的!
索引
Using Table Aliases
- Using aliases for column names and calculated fields
To shorten the SQL syntax
To enable multiple uses of the same table within a single SELECT statement
自交
像下面这样的叫做“自交” ↓
- SELECT p1.prod_id, p1.prod_name
- FROM products AS p1, products AS p2
- WHERE p1.vend_id = p2.vend_id
- AND p2.prod_id = 'DTNTR';
Self Joins Instead of Subqueries Self joins are often used to replace statements using subqueries that retrieve data from the same table as the outer statement. Although the end result is the same, sometimes these joins execute far more quickly than they do subqueries. It is usually worth experimenting with both to determine which performs better.
接下来用于实验的两张表:
- -- master
+-----------+-------------+- | master_id | master_name |
- +-----------+-------------+
- | | 王二牠 |
- | | 李明顠 |
- | | 田中吠 |
- | | 陆大襠 |
- +-----------+-------------+
- -- pet
+--------+----------+----------+-----------+- | pet_id | pet_type | pet_name | master_id |
- +--------+----------+----------+-----------+
- | | NULL | 飿¡¶ | |
- | | dog | 小白 | |
- | | cat | 老黄 | |
- +--------+----------+----------+-----------+
无约束自交,原来 3 条 结果 3 × 3 ↓
- mysql> SELECT *
- -> FROM pet AS p1, pet AS p2;
- +--------+----------+----------+-----------+--------+----------+----------+-----------+
- | pet_id | pet_type | pet_name | master_id | pet_id | pet_type | pet_name | master_id |
- +--------+----------+----------+-----------+--------+----------+----------+-----------+
- | 8881 | NULL | 飿¡¶ | 1001 | 8881 | NULL | 飿¡¶ | 1001 |
- | 8882 | dog | 小白 | 1002 | 8881 | NULL | 飿¡¶ | 1001 |
- | 8883 | cat | 老黄 | 1003 | 8881 | NULL | 飿¡¶ | 1001 |
- | 8881 | NULL | 飿¡¶ | 1001 | 8882 | dog | 小白 | 1002 |
- | 8882 | dog | 小白 | 1002 | 8882 | dog | 小白 | 1002 |
- | 8883 | cat | 老黄 | 1003 | 8882 | dog | 小白 | 1002 |
- | 8881 | NULL | 飿¡¶ | 1001 | 8883 | cat | 老黄 | 1003 |
- | 8882 | dog | 小白 | 1002 | 8883 | cat | 老黄 | 1003 |
- | 8883 | cat | 老黄 | 1003 | 8883 | cat | 老黄 | 1003 |
- +--------+----------+----------+-----------+--------+----------+----------+-----------+
自然交。。
就是可以自动消除相同字段的一种交,但是 MySQL 并没有实现这种交,如果你 像下面这样查 就会看到好多相同字段:
- SELECT *
- FROM customers AS c, orders AS o, orderitems AS oi
- WHERE c.cust_id = o.cust_id
- AND oi.order_num = o.order_num
- AND prod_id = 'FB';
| cust_id | cust_name | cust_address | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email | order_num | order_date | cust_id | order_num | order_item | prod_id | quantity | item_price |
所以,要实现自然交就 只能自己具体指明字段了:
- SELECT c.*, o.order_num, o.order_date,
- oi.prod_id, oi.quantity, OI.item_price
- FROM customers AS c, orders AS o, orderitems AS oi
- WHERE c.cust_id = o.cust_id
- AND oi.order_num = o.order_num
- AND prod_id = 'FB';
MySQL 不实现自然交:
- mysql> SELECT *
- -> FROM master AS m, pet AS p
- -> WHERE m.master_id = p.master_id;
- +-----------+-------------+--------+----------+----------+-----------+
- | master_id | master_name | pet_id | pet_type | pet_name | master_id |
- +-----------+-------------+--------+----------+----------+-----------+
- | 1001 | 王二牠 | 8881 | NULL | 飿¡¶ | 1001 |
- | 1002 | 李明顠 | 8882 | dog | 小白 | 1002 |
- | 1003 | 田中吠 | 8883 | cat | 老黄 | 1003 |
- +-----------+-------------+--------+----------+----------+-----------+
- 3 rows in set (0.00 sec)
实现自然交:
- mysql> SELECT master_name, p.*
- -> FROM master AS m, pet AS p
- -> WHERE m.master_id = p.master_id;
- +-------------+--------+----------+----------+-----------+
- | master_name | pet_id | pet_type | pet_name | master_id |
- +-------------+--------+----------+----------+-----------+
- | 王二牠 | 8881 | NULL | 飿¡¶ | 1001 |
- | 李明顠 | 8882 | dog | 小白 | 1002 |
- | 田中吠 | 8883 | cat | 老黄 | 1003 |
- +-------------+--------+----------+----------+-----------+
PS. FROM 里面 AS 的假名在 SELECT 中是可以用的。
Outer Joins
The join includes table rows that have no associated rows in the related table. This type of join is called an outer join. But unlike inner joins, which relate rows in both tables, outer joins also include rows with no related rows.
在讲到 OUTER JOIN 的同时就不得不提到 INNER JOIN ,推荐阅读这篇文章 Inner Join vs. Outer Join
真 · INNER JOIN ↓
- mysql> SELECT master_name, p.*
- -> FROM master AS m INNER JOIN pet AS p
- -> ON m.master_id = p.master_id;
- +-------------+--------+----------+----------+-----------+
- | master_name | pet_id | pet_type | pet_name | master_id |
- +-------------+--------+----------+----------+-----------+
- | 王二牠 | 8881 | NULL | 飿¡¶ | 1001 |
- | 李明顠 | 8882 | dog | 小白 | 1002 |
- | 田中吠 | 8883 | cat | 老黄 | 1003 |
- +-------------+--------+----------+----------+-----------+
因为我个人认为没有人会需要 X * X 的 那种表 所以 自己 把 那种 不加 ON 后面的相等约束 的 表 叫 伪 · INNER JOIN
LEFT OUTER JOIN ↓
- mysql> SELECT master_name, p.*
- -> FROM master AS m LEFT OUTER JOIN pet AS p
- -> ON m.master_id = p.master_id;
- +-------------+--------+----------+----------+-----------+
- | master_name | pet_id | pet_type | pet_name | master_id |
- +-------------+--------+----------+----------+-----------+
- | 王二牠 | 8881 | NULL | 飿¡¶ | 1001 |
- | 李明顠 | 8882 | dog | 小白 | 1002 |
- | 田中吠 | 8883 | cat | 老黄 | 1003 |
- | 陆大襠 | NULL | NULL | NULL | NULL |
- +-------------+--------+----------+----------+-----------+
RIGHT OUTER JOIN ↓
- mysql> SELECT master_name, p.*
- -> FROM master AS m RIGHT OUTER JOIN pet AS p
- -> ON m.master_id = p.master_id;
- +-------------+--------+----------+----------+-----------+
- | master_name | pet_id | pet_type | pet_name | master_id |
- +-------------+--------+----------+----------+-----------+
- | 王二牠 | 8881 | NULL | 飿¡¶ | 1001 |
- | 李明顠 | 8882 | dog | 小白 | 1002 |
- | 田中吠 | 8883 | cat | 老黄 | 1003 |
- +-------------+--------+----------+----------+-----------+
PS. the two types of outer join can be used interchangeably, and the decision about which one is used is based purely on convenience.
MySQL Crash Course #08# Chapter 16. Using Different Join Types的更多相关文章
- MySQL Crash Course #07# Chapter 15. 关系数据库. INNER JOIN. VS. nested subquery
索引 理解相关表. foreign key JOIN 与保持参照完整性 关于JOIN 的一些建议,子查询 VS. 联表查询 我发现MySQL 的官方文档里是有教程的. SQL Tutorial - W ...
- MySQL Crash Course #15# Chapter 23. Working with Stored Procedures
以前写过类似的东西,用来自动生成数据. 你可以将 Stored Procedure 理解为可以重复使用的批处理文件. Stored Procedure 非常有用,我们应该尽可能地去使用它. 那么,应用 ...
- MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
- MySQL Crash Course #11# Chapter 20. Updating and Deleting Data
INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...
- MySQL Crash Course #10# Chapter 19. Inserting Data
INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...
- MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询
索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...
- MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE
索引 AND. OR 运算顺序 IN Operator VS. OR NOT 在 MySQL 中的表现 LIKE 之注意事项 运用通配符的技巧 Understanding Order of Evalu ...
- MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
- MySQL Crash Course #20# Chapter 28. Managing Security
限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...
随机推荐
- Redis的启动过程
本文主要介绍Redis启动加载过程,总体上可以分为如下几步: 1. 初始化全局服务器配置 2. 加载配置文件(如果指定了配置文件,否则使用默认配置) 3. 初始化服务器 4. 加载数据库 5. 网络监 ...
- 模拟线程安全的售票案例(java)
package try51.thread.safe; import java.util.ArrayList; import java.util.Random; import java.util.con ...
- Java-字符串加密
1设计思想: 改程序是对小写的a到z进行加密,输入一段字符串str,输入加密的密匙k,判断录入的的字符与 ‘z’-k+1的大小,比其小的直接加上密匙转化为新的字符,大于的加(k-26)将最后几位字符转 ...
- css中:hover空格
前面有空格后代所有节点,前面无空格第一个节点 <div class="task-item"> <span><input type="chec ...
- 43(function pointer 1)
#include<iostream> using namespace std; typedef int A; typedef void (*PF)(); typedef int (*P_A ...
- JZ2440裸板烧写(打补丁)
制作uImage,需要上网下载内核+patch补丁 1.将内核用ftp发送到 打补丁patch -p1 < ../补丁文件名 打补丁文件(目录) 2.配置 :复制cp configuratio ...
- zookeeper 版本不一致导致不断重连
在使用kafka 和zookeeper 实现实时分析程序时,由于zookeeper部署版本和分析程序导入jar包的版本不一致,导致了当实时分析程序从远程服务器连接kafka集群的zookeeper时报 ...
- vmware 安装 kali linux 系统到U盘 启动错误(initramfs:) 修复方法
安装kalilinux到U盘 启动之后出现这个错误: 这是grub路径不对. 解决办法: 在这模式下可以输入命令:blkid 查看所列举出的磁盘 找到你的U盘系统 TYPE="ext4&qu ...
- util date 转 sql date
JAVA 处理时间 - java.sql.Date.java.util.Date与数据库中的Date字段的转换方法,以及util包下的Date类与字符串的相互转换在java环境中使用的Date时间类通 ...
- 简述ASP.NET的页面运行机制
在深入学习ASP.NET之前,首先需要先了解一下ASP.NET的页面运行机制: 浏览以下内容需要对ASP.NET管道有一定的了解,附上很不错的几个链接: 选择HttpHandler还是HttpModu ...