记文档还是相当重要的!

索引

  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. FZU 2092 收集水晶(记忆化搜索)

    Problem 2092 收集水晶 Accept: 101 Submit: 439 Time Limit: 5000 mSec Memory Limit : 32768 KB Problem Desc ...

  2. poj3347 Kadj Squares【计算几何】

    Kadj Squares Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3594   Accepted: 1456 Desc ...

  3. PAT甲1005 Spell it right【字符串】

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  4. OpenCV学习笔记之课后习题练习4-1

    第四章课后练习1 1.本章完整讲述了基本的输入/输出编程以及OpenCV的数据结构.下面的练习是基于前面的知识做一些应用,为后面大程序的实现提供帮助.a.创建一个程序实现以下功能:(1)从视频文件中读 ...

  5. 0003python中的可变参数

    >>>def foo(x,y,z,*args,**kargs): print x print y print z print args print kargs >>> ...

  6. Python:zip()函数

    zip()函数的定义 从参数中的多个迭代器取元素组合成一个新的迭代器: 返回:返回一个zip对象,其内部元素为元组:可以转化为列表或元组: 传入参数:元组.列表.字典等迭代器. zip()函数的用法 ...

  7. Python高阶函数:map、reduece、filter

    笔记中函数简介: map函数:遍历序列,对序列中每个元素进行操作,最终获取新的序列. reduce函数:对于序列内所有元素进行累计操作. filter函数:对于序列中的元素进行筛选,最终获取符合条件的 ...

  8. 东哥讲义2 - 基于TCP,UDP协议的攻击,分析与防护

    TCP SYN FLOOD 攻击 正常的TCP三次握手过程: 处于SYN FLOOD攻击状态时的三次握手过程: 查看示例:x_syn.c文件,一个实现了自定义mac,ip,tcp头部的syn floo ...

  9. java web启动后执行初始化任务

    写一个类继承ApplicationListener,可以直接引用下述代码,然后调用相应的方法. package com.linewell.system; import com.linewell.cac ...

  10. elasticsearch数据转移,elasticdump的安装使用

    模拟: 将本地的my_index的products的一条document转移到http://192.168.111.130的一个es服务器上. (一)安装elasticdump 先安装node.js, ...