右外连接类似于Union
维恩图,对吗?
我的意思是我们A right outer Join B
应该得到.
出于某种原因,我对以下内容感到困惑:
假设表: B
A
Orders
mysql> select * from orders;
+------------+------------+---------+----------+---------+
| orderedon | name | partnum | quantity | remarks |
+------------+------------+---------+----------+---------+
| 1996-05-19 | TRUE-WHEEL | 76 | 3 | PAID |
| 1996-09-02 | TRUE-WHEEL | 10 | 1 | PAID |
| 1996-06-30 | TRUE-WHEEL | 42 | 8 | PAID |
| 1996-06-30 | BIKE SPEC | 54 | 10 | PAID |
| 1996-05-30 | BIKE SPEC | 23 | 8 | PAID |
| 1996-01-17 | BIKE SPEC | 76 | 11 | PAID |
| 1996-01-17 | LE SHOPPE | 76 | 5 | PAID |
| 1996-06-01 | LE SHOPPE | 10 | 3 | PAID |
| 1996-06-01 | AAA BIKE | 10 | 1 | PAID |
| 1996-07-01 | AAA BIKE | 76 | 4 | PAID |
| 1996-07-01 | AAA BIKE | 46 | 14 | PAID |
| 1996-07-11 | JACKS BIKE | 76 | 14 | PAID |
| 1996-05-15 | TRUE-WHEEL | 23 | 6 | PAID |
| 1996-05-30 | BIKE SPEC | 20 | 2 | PAID |
+------------+------------+---------+----------+---------+
14 rows in set (0.00 sec)
和表Part
:
mysql> select * from part;
+---------+---------------+---------+
| partnum | description | price |
+---------+---------------+---------+
| 54 | PEDALS | 54.25 |
| 42 | SEATS | 24.50 |
| 46 | TIRES | 15.25 |
| 23 | MOUNTAIN BIKE | 350.45 |
| 76 | ROAD BIKE | 530.00 |
| 10 | TANDEM | 1200.00 |
+---------+---------------+---------+
6 rows in set (0.00 sec)
我期待下面的查询:
select p.partnum p_partnum,p.description p_desc,p.price p_price,o.name o_name,o.partnum o_partnum from part p right outer join orders o on o.partnum=54;
会给我所有的行Orders
和只有那些的part
行partnum=54
。
但我明白了:
mysql> select p.partnum p_partnum,p.description p_desc,p.price p_price,o.name o_name,o.partnum o_partnum from part p right outer join orders o on o.partnum=54;
+-----------+---------------+---------+------------+-----------+
| p_partnum | p_desc | p_price | o_name | o_partnum |
+-----------+---------------+---------+------------+-----------+
| NULL | NULL | NULL | TRUE-WHEEL | 76 |
| NULL | NULL | NULL | TRUE-WHEEL | 10 |
| NULL | NULL | NULL | TRUE-WHEEL | 42 |
| 54 | PEDALS | 54.25 | BIKE SPEC | 54 |
| 42 | SEATS | 24.50 | BIKE SPEC | 54 |
| 46 | TIRES | 15.25 | BIKE SPEC | 54 |
| 23 | MOUNTAIN BIKE | 350.45 | BIKE SPEC | 54 |
| 76 | ROAD BIKE | 530.00 | BIKE SPEC | 54 |
| 10 | TANDEM | 1200.00 | BIKE SPEC | 54 |
| NULL | NULL | NULL | BIKE SPEC | 23 |
| NULL | NULL | NULL | BIKE SPEC | 76 |
| NULL | NULL | NULL | LE SHOPPE | 76 |
| NULL | NULL | NULL | LE SHOPPE | 10 |
| NULL | NULL | NULL | AAA BIKE | 10 |
| NULL | NULL | NULL | AAA BIKE | 76 |
| NULL | NULL | NULL | AAA BIKE | 46 |
| NULL | NULL | NULL | JACKS BIKE | 76 |
| NULL | NULL | NULL | TRUE-WHEEL | 23 |
| NULL | NULL | NULL | BIKE SPEC | 20 |
+-----------+---------------+---------+------------+-----------+
19 rows in set (0.00 sec)
为什么我会得到额外的行?为什么它将Order
withpartnum=54
的行与 `part 的所有行结合起来?