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 ...
随机推荐
- 《modern-php》 - 阅读笔记 - 最佳实践
过滤.验证和转义数据 过滤数据 不要相信任何外部数据! 常见的有以下几种数据需要过滤:HTML,SQL查询,用户提交的信息(邮件地址.电话号码.身份证) HTML htmlentities() HTM ...
- 从本机IIS中管理 远程服务器 IIS
有时候,一般情况下,我们对服务器上 IIS 上的管理局限于 使用远程桌面:现在介绍一种,通过 本机 管理管理远程IIS 的方法! 1. 服务器端设置: 服务器管理器 ==>增加角色和功能向导= ...
- FZU - 2150 Fire Game bfs+双起点枚举
题意,10*10的地图,有若干块草地“#”,草地可以点燃,并在一秒后点燃相邻的草地.有墙壁‘·‘阻挡.初始可以从任意两点点火.问烧完最短的时间.若烧不完输出-1. 题解:由于100的数据量,直接暴力. ...
- 数据恢复:如何恢复Linux中意外删除的Oracle和MySQL数据库
今天有客户的数据库意外被删除了整个目录中的数据文件,操作系统级别的删除,然而幸运的是这个数据库没有崩溃,仍然处于 open 状态的时候,客户就发现了问题,求助到我们,最终完整地恢复了所有数据文件. 在 ...
- intptr_t、uintptr_t数据类型的解析
https://blog.csdn.net/cs_zhanyb/article/details/16973379 2013年11月26日 22:20:09 binggo 阅读数:14066 最近开 ...
- java.io.File实战
There are many things that can go wrong: A class works in Unix but doesn't on Windows (or vice versa ...
- 了解Linux的进程与线程
了解Linux的进程与线程 http://timyang.net/linux/linux-process/ 上周碰到部署在真实服务器上某个应用CPU占用过高的问题,虽然经过tuning, 问题貌似已经 ...
- 网站用sqlite库,报attempt to write a readonly database,解决方法
将sqlite数据库文件,设置为users完全控制.重启网站即可!
- easyUI中datebox的格式显示
使用datebox的问题: 1.需要YYYY-MM-dd这种时间格式: 2.月份显示的是中文. 上述两个问题只要引入国际化的js文件即可. 注:下图为easyUI使用时需要引入的文件,红框就可以解决上 ...
- mysql 实时统计脚本 QPS,TPS和线程连接数等
#!/bin/bash mysqladmin -uroot -p'root' extended-status -i1|awk 'BEGIN{local_switch=0;print "QPS ...