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 ...
随机推荐
- select、poll和epoll的比较
一.select机制 在linux下网络通信中,经常用到select机制,这是一种异步通信的实现方式,select中提供一fd_set的数据结果,实际上是一个long类型的数组, 每一个数组元素都能与 ...
- poj3417Network【LCA】【树形DP】
Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...
- webpack笔记二——entry
entry是输入目录文件,有三种形式 1.对象键值对形式 entry: { main: './src/script/main.js', b: './src/script/b.js' }, 注意的是输出 ...
- oracle显示转换字段类型cast()函数
今天遇到一个查询类型转换的问题:表的字段是varchar2类型,然后查询到的结果要转换为number(20,2),刚开始的时候使用to_number()函数,发现不能满足需求.后来才知道,原来还有ca ...
- 【CSS3】CSS3自学
CSS3学习网址:http://www.runoob.com/css3/css3-tutorial.html
- BUG笔记:Win8 IE10下input[type="password"]内字符显示被截取问题
这个BUG发生的截图: 这是发生在Windows8 IE10下,type为password的input文本框内输入长串字符后,初次失去焦点的时候会发生的一个BUG. 发生BUG的原因是这个文本框上应用 ...
- 【深入理解javascript】执行上下文
参考原文:执行上下文 1.每一个执行上下文,工作分为三个阶段: 准备阶段–>执行阶段–>调用阶段 准备阶段:代码执行之前,设置数据,相当于初始化. 执行阶段:开始执行每一行代码. 调用阶段 ...
- byte处理的几种方法
/** * 字符串转16进制byte * @param * @return * @throws Exception * @author hw * @date 2018/10/19 9:47 */ pr ...
- Flask中'endpoint'(端点)的理解
翻译整理自Stack Overflow:http://stackoverflow.com/questions/19261833/what-is-an-endpoint-in-flask 原文中用到了m ...
- Biorhythms(中国剩余定理)
http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...