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 ...
随机推荐
- 【紫书】【重要】Abbott's Revenge UVA - 816 bfs 复杂模拟 带方向参数的迷宫
题意:一个迷宫,每个交叉路口有一路标,限制了你从某方向进入该路口所能进入的路口. 题解:1.对于方向的处理:将node多增加一维dir,通过一个const 字符数组 加 上dir_id函数 以及一个方 ...
- hue安装及基本测试-笔记
#################################################################################################### ...
- iOS + Node + MySQL
最近有空,又温习了一下Node ,配合Express 4.x可以很快的搭建一个简单的后台. Node比较适合频繁I/O,大量异步.至于更加复杂的后台逻辑还是用Java,个中滋味自己体验. Expres ...
- UITableView _endCellAnimationsWithContext崩溃
http://www.cocoachina.com/bbs/read.php?tid-315222.html 删除cell之前先把数据源也删除一条,直接删除cell会崩溃 下面是正确的姿势: cell ...
- 迷宫城堡--hdu1269(连通图)
题目链接 连通图模板题: #include<cstdio> #include<cstdlib> #include<cmath> #include<iost ...
- maven国内稳定的阿里源
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...
- gulp处理错误
Gulp 目前的错误处理方式有点操蛋,一旦发生错误进程就挂了,得手动去重启.虽然开发者预期在 gulp 4 中解决此问题 ,但 gulp 4 什么时候发布并没有明确时间表,在此之前,还是很有必要了解一 ...
- 前端 HTML body标签相关内容 常用标签 换行标签 br
换行标签 <br> <br>标签用来将内容换行,其在HTML网页上的效果相当于我们平时使用word编辑文档时使用回车换行. 在第一行中间加上br <!DOCTYPE ht ...
- [MySQL5.6] 最近对group commit的小优化
[MySQL5.6] 最近对group commit的小优化 http://www.tuicool.com/articles/rEZr2q 最近花了一些时间在做MySQL Group Commit的优 ...
- html02
复习:HTML标记 p h1~h6 font table>tr>td ul>li ol>li div span form:input>typy :password rad ...