MySQL - LEFT JOIN and RIGHT JOIN, INNER JOIN and OUTER JOIN

本文通过实例解释了数据库中JOIN的不同类型,包括INNER JOIN、LEFT JOIN和RIGHT JOIN的区别及应用场景。

--网上找到一篇介绍join的很简单,和大家分享
在数据库中, 数据是被分别放在不同的表里面的,通过SELECT 语句把不同表里面的数据连接在一起,按照需要的条件生成输出。大家经常搞不懂join这个语法,所以接下来就详细的进行说明。

首先是一些样例数据,表一demo_people
Mr Brown, Person number 1, has a phone number 01225 708225
Miss Smith, Person number 2, has a phone number 01225 899360
Mr Pullen, Person number 3, has a phone number 01380 724040


表2 demo_property
Person number 1 is selling property number 1 - Old House Farm
Person number 3 is selling property number 2 - The Willows
Person number 3 is (also) selling property number 3 - Tall Trees
Person number 3 is (also) selling property number 4 - The Melksham Florist
Person number 4 is selling property number 5 - Dun Roamin.

mysql> select * from demo_people;

+------------+--------------+------+

| name       | phone        | pid  |

+------------+--------------+------+

| Mr Brown   | 01225 708225 |    1 |

| Miss Smith | 01225 899360 |    2 |

| Mr Pullen  | 01380 724040 |    3 |

+------------+--------------+------+

3 rows in set (0.00 sec)



mysql> select * from demo_property;

+------+------+----------------------+

| pid  | spid | selling              |

+------+------+----------------------+

|    1 |    1 | Old House Farm       |

|    3 |    2 | The Willows          |

|    3 |    3 | Tall Trees           |

|    3 |    4 | The Melksham Florist |

|    4 |    5 | Dun Roamin           |

+------+------+----------------------+

5 rows in set (0.00 sec)



mysql>


如果用一般的JOIN(不用INNER, OUTER, LEFT or RIGHT关键字中的任何一个),将从两张表中得对符合条件的结果。其他不符合条件的记录将不能输出。

mysql> select name, phone, selling

from demo_people join demo_property
on demo_people.pid = demo_property.pid;



+-----------+--------------+----------------------+

| name      | phone        | selling              |

+-----------+--------------+----------------------+

| Mr Brown  | 01225 708225 | Old House Farm       |

| Mr Pullen | 01380 724040 | The Willows          |

| Mr Pullen | 01380 724040 | Tall Trees           |

| Mr Pullen | 01380 724040 | The Melksham Florist |

+-----------+--------------+----------------------+

4 rows in set (0.01 sec)



mysql>




如果用LEFT JOIN,将得到和上面一般JOIN一样的结果,并且从左边的表中得到一条额外的记录。也就是说左连接保证了左边的表中所有的人都被提及。

mysql> select name, phone, selling 
from demo_people left join demo_property
on demo_people.pid = demo_property.pid;
+------------+--------------+----------------------+
| name | phone | selling |
+------------+--------------+----------------------+
| Mr Brown | 01225 708225 | Old House Farm |
| Miss Smith | 01225 899360 | NULL |
| Mr Pullen | 01380 724040 | The Willows |
| Mr Pullen | 01380 724040 | Tall Trees |
| Mr Pullen | 01380 724040 | The Melksham Florist |
+------------+--------------+----------------------+
5 rows in set (0.00 sec)

mysql>

如果用RIGHT JOIN,将得到和上面一般JOIN一样的结果,并且从右边的表中得到一条额外的记录。在这个例子中表示虽然没有指定的卖家,但是所有的商品都将显示出来


mysql> select name, phone, selling
from demo_people right join demo_property
on demo_people.pid = demo_property.pid;
+-----------+--------------+----------------------+
| name | phone | selling |
+-----------+--------------+----------------------+
| Mr Brown | 01225 708225 | Old House Farm |
| Mr Pullen | 01380 724040 | The Willows |
| Mr Pullen | 01380 724040 | Tall Trees |
| Mr Pullen | 01380 724040 | The Melksham Florist |
| NULL | NULL | Dun Roamin |
+-----------+--------------+----------------------+
5 rows in set (0.00 sec)

mysql>



An INNER JOIN does a full join, just like the first example, and the word OUTER may be added after the word LEFT or RIGHT in the last two examples - it's provided for ODBC compatibility and doesn't add an extra capabilities.

 INNER JOIN 和 full join,功能一样, OUTER JOIN 放在LEFT or RIGHT 后面。结果不变

 

原文:http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html


MySQL中的JOIN操作用于将两个或更多表连接在一起,以便基于它们之间的共同字段获取数据。主要有三种类型的JOIN: ### 内连接 (INNER JOIN) 内连接返回的是来自参与联接的两个表的所有匹配行。如果其中一个表中没有匹配项,则不会包含在结果集中。 **语法示例**: ```sql SELECT table1.column1, table2.column2 FROM table1 INNER JOIN table2 ON table1.common_column = table2.common_column; ``` 这个查询将从`table1`和`table2`中选择所有在`common_column`列上相等的行,并显示`table1.column1`和`table2.column2`这两列的数据。 ### 左外联接 (LEFT JOINLEFT OUTER JOIN) 左外联接返回左侧表的所有行及其匹配的右侧表的行,如果没有匹配则由NULL填充右侧表的部分。 **语法示例**: ```sql SELECT table1.column1, table2.column2 FROM table1 LEFT JOIN table2 ON table1.common_column = table2.common_column; ``` 这将从`table1`中选择所有的行,以及可能从`table2`中匹配的行。如果某个`table1`行没有匹配到`table2`,其与`table2.column2`相关的部分将是NULL。 ### 右外联接 (RIGHT JOINRIGHT OUTER JOIN) 右外联接与左外联接类似,但返回右侧表的所有行及其匹配的左侧表的行。如果没有匹配,则由NULL填充左侧表的部分。 **语法示例**: ```sql SELECT table1.column1, table2.column2 FROM table1 RIGHT JOIN table2 ON table1.common_column = table2.common_column; ``` 这将从`table2`中选择所有的行,以及可能从`table1`中匹配的行。如果某个`table2`行没有匹配到`table1`,其与`table1.column1`相关的部分将是NULL。 ### 相关问题: 1. MySQL JOIN 的应用场景有哪些? 2. 如何优化JOIN查询以提高性能? 3. MySQLJOIN与UNION的区别是什么?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值