0

右外连接类似于Union维恩图,对吗?
我的意思是我们A right outer Join B应该得到. 出于某种原因,我对以下内容感到困惑: 假设表: BA

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和只有那些的partpartnum=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)   

为什么我会得到额外的行?为什么它将Orderwithpartnum=54的行与 `part 的所有行结合起来?

4

4 回答 4

3

在此处输入图像描述

您的查询是

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;  

您将进行右连接,因此即使右侧的连接表中没有匹配项,它也会显示记录,并且在您的查询的这种情况下会发生这种情况

左连接也是如此,即使它们不匹配,也会显示左侧表的所有记录

参考http://www.w3schools.com/sql/sql_join_right.asp

如需详细说明

希望这可以帮助

于 2013-02-15T10:09:30.523 回答
1

因为您必须RIGHT JOIN使用ordersjoin 条件,所以partnum=54您将从join orderswith partswhen中获取所有行partnum=54。您有一行,partnum=54并且该行与来自 的所有行(交叉连接)连接parts

于 2013-02-15T09:57:51.613 回答
1
FROM part p 
RIGHT OUTER JOIN orders o 
  ON o.partnum=54

...仅对订单有条件,您需要添加的条件是该部分也与该订单对应,否则数据库将认为任何部分都匹配;

FROM part p 
RIGHT OUTER JOIN orders o 
  ON o.partnum=54
 AND o.partnum = p.partnum

当然,如果您只想显示 partnum=54 的行,最好将 移动o.partnum=54到一个WHERE条件。JOIN条件通常用于连接表,WHERE通常用于过滤。

于 2013-02-15T09:58:29.343 回答
1

您正在使用o.partnum = 54on ON condition,这就是您在结果中获得额外行的原因。

您需要将条件o.partnum = 54放在 where 子句上。

这个怎么样

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 = p.partnum
where o.partnum=54;

编辑:您可以参考这篇漂亮的文章,当您在 ON 子句和 Where 上设置条件时结果集如何受到干扰。

于 2013-02-15T09:58:53.163 回答