【问题标题】:MySQL multiple right joinsMySQL 多重右连接
【发布时间】:2009-05-14 19:49:48
【问题描述】:
mysql> select * from product;
+------------+---------------+
| product_id | name          |
+------------+---------------+
|          1 | Car           | 
|          2 | House         | 
|          3 | Cat           | 
|          4 | Blank Product | 
+------------+---------------+
4 rows in set (0.00 sec)

mysql> select * from tag;
+--------+-----------+
| tag_id | name      |
+--------+-----------+
|      1 | Expensive | 
|      2 | Fast      | 
|      3 | Mean      | 
|      4 | Large     | 
|      5 | Small     | 
|      6 | Alive     | 
|      7 | Blank Tag | 
+--------+-----------+
7 rows in set (0.00 sec)

mysql> select * from product_tag;
+------------+--------+
| product_id | tag_id |
+------------+--------+
|          1 |      1 | 
|          1 |      2 | 
|          1 |      3 | 
|          1 |      4 | 
|          2 |      1 | 
|          2 |      4 | 
|          3 |      2 | 
|          3 |      3 | 
|          3 |      5 | 
|          3 |      6 | 
+------------+--------+
10 rows in set (0.00 sec)

为什么下面的查询返回我的空白标签而不是我的空白产品?

mysql> select * from product_tag right join product using (product_id)
                                 right join tag using (tag_id);
+--------+-----------+------------+-------+
| tag_id | name      | product_id | name  |
+--------+-----------+------------+-------+
|      1 | Expensive |          1 | Car   | 
|      1 | Expensive |          2 | House | 
|      2 | Fast      |          1 | Car   | 
|      2 | Fast      |          3 | Cat   | 
|      3 | Mean      |          1 | Car   | 
|      3 | Mean      |          3 | Cat   | 
|      4 | Large     |          1 | Car   | 
|      4 | Large     |          2 | House | 
|      5 | Small     |          3 | Cat   | 
|      6 | Alive     |          3 | Cat   | 
|      7 | Blank Tag |       NULL | NULL  | 
+--------+-----------+------------+-------+
11 rows in set (0.00 sec)

【问题讨论】:

    标签: mysql join


    【解决方案1】:

    您正在使用右连接。在您的查询标签中,id 是 MySQL 开始匹配的基础。右连接从右到左进行评估。如果您将查询分为两部分。第一个将是:

    select * from product_tag right join tag using (tag_id);
    +--------+-----------+------------+
    | tag_id | name      | product_id |
    +--------+-----------+------------+
    |      1 | expensive |          1 | 
    |      1 | expensive |          2 | 
    |      2 | fast      |          1 | 
    |      2 | fast      |          3 | 
    |      3 | mean      |          1 | 
    |      3 | mean      |          3 | 
    |      4 | larg      |          1 | 
    |      4 | larg      |          2 | 
    |      5 | small     |          3 | 
    |      6 | alive     |          3 | 
    |      7 | blank tag |       NULL | 
    
    +--------+-----------+------------+
    

    如您所见,没有与空白标签匹配的 product_id。解释了为什么将此结果与产品表连接会得到您看到的结果。

    如果你改用左连接,你会得到这个结果:

    select * from product_tag left join product using (product_id) left join tag using (tag_id);
    
    +--------+------------+-------+-----------+
    | tag_id | product_id | name  | name      |
    +--------+------------+-------+-----------+
    |      1 |          1 | car   | expensive | 
    |      2 |          1 | car   | fast      | 
    |      3 |          1 | car   | mean      | 
    |      4 |          1 | car   | larg      | 
    |      1 |          2 | house | expensive | 
    |      4 |          2 | house | larg      | 
    |      2 |          3 | cat   | fast      | 
    |      3 |          3 | cat   | mean      | 
    |      5 |          3 | cat   | small     | 
    |      6 |          3 | cat   | alive     | 
    +--------+------------+-------+-----------+
    

    【讨论】:

    • 很好的答案。非常详细的+1
    【解决方案2】:

    没有将产品 ID 4 与标签相关联的行。您需要向 product_tag 表中添加一行,如下所示:

    +------------+--------+
    | product_id | tag_id |
    +------------+--------+
    |          4 |      7 | 
    +------------+--------+
    

    【讨论】:

    • 没有将标签 7(空白标签)与任何产品相关联的行,但我的右连接返回它。
    【解决方案3】:

    您正在使用 RIGHT JOIN,因此即使连接的表中没有匹配项,也会返回右表中的所有行,即“标记”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 2012-02-10
      • 1970-01-01
      相关资源
      最近更新 更多