记文档还是相当重要的!

索引

  1. 假名的三个用途
  2. 自交(Self Joins)
  3. 自然交(Natural Joins)
  4. Outer Joins

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的更多相关文章

  1. MySQL Crash Course #07# Chapter 15. 关系数据库. INNER JOIN. VS. nested subquery

    索引 理解相关表. foreign key JOIN 与保持参照完整性 关于JOIN 的一些建议,子查询 VS. 联表查询 我发现MySQL 的官方文档里是有教程的. SQL Tutorial - W ...

  2. MySQL Crash Course #15# Chapter 23. Working with Stored Procedures

    以前写过类似的东西,用来自动生成数据. 你可以将 Stored Procedure 理解为可以重复使用的批处理文件. Stored Procedure 非常有用,我们应该尽可能地去使用它. 那么,应用 ...

  3. MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables

    之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...

  4. 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 ...

  5. MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...

  6. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  7. 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 ...

  8. MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance

    终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...

  9. MySQL Crash Course #20# Chapter 28. Managing Security

    限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...

随机推荐

  1. Spark Streaming 在数据平台日志解析功能的应用

    https://mp.weixin.qq.com/s/bGXhC9hvDj4lzK7wYYHGDg 目前,我们使用Filebeat监控日志产生的目录,收集产生的日志,打到logstash集群,接入ka ...

  2. https://validator.w3.org

    https://validator.w3.org/nu/?doc=http%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.7%2Fen%2Fmanual-info. ...

  3. spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法

    前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以 ...

  4. 伪列ROWNUM、ROWID部分用法

    ROWNUM是逻辑值,不可以参与计算 ROWID是物理值,可以参与计算 在ROWNUM和ROWID使用中,现将查找结果形成一个结果集 在结果集中给ROWID 和ROWNUM别名,在外层中使用这个别名找 ...

  5. 【Pyton】【小甲鱼】魔法方法

    1.__init__ >>> class Rectangle: def __init__(self,x,y): self.x=x self.y=y def getPeri(self) ...

  6. 图结构练习——判断给定图是否存在合法拓扑序列(sdutoj)

    #include<stdio.h>#include<string.h>int d[15],map[15][15],vis[15];int main(){    int i,j, ...

  7. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  8. Apache下设置网站目录的访问权限

    禁止用户对某一个目录及目录下文件的访问,仅允许本地访问 <Directory "/wwwroot/cert/"> Require local </Director ...

  9. 因子分析(Factor Analysis)

    原文地址:http://www.cnblogs.com/jerrylead/archive/2011/05/11/2043317.html 1 问题 之前我们考虑的训练数据中样例的个数m都远远大于其特 ...

  10. “在引用COM组件时,出现了无法嵌入互操作类型。。。”的错误

    这两天在做一个需要将wps文档转换成word文档的程序,在调用wps的com组件时项目编译是没有问题的,但当运行的时候却弹出了下面的错误提示: 从网上百度一番后,找到了正确的解决方法. 先从Com组件 ...