【问题标题】:MySQL query is returning duplicates when it shouldn'tMySQL查询不应该返回重复项
【发布时间】:2013-06-19 20:29:16
【问题描述】:

我有三张桌子:

人

+--------+-----------+  
| fName  | lName     |  
+--------+-----------+  
| Paul   | McCartney |  
| John   | Lennon    |  
| Jon    | Stewart   |  
| Daniel | Tosh      |  
| Steven | Colbert   |  
| Pink   | Floyd     |  
| The    | Beatles   |  
| Arcade | Fire      |  
| First  | Last      |  
| Andrew | Bird      |  
+--------+-----------+  

出版

+----+---------------------------------------+------+-----------+---------+  
| id | title                                 | year | pageStart | pageEnd |  
+----+---------------------------------------+------+-----------+---------+  
|  9 | The Dark Side of the Moon             | 1973 |         0 |       0 |  
| 10 | Piper At The Gates of Dawn            | 1967 |         0 |       0 |  
| 11 | Sgt. Pepper's Lonely Hearts Band Club | 1967 |         0 |       0 |  
| 12 | Happy Thoughts                        | 2007 |         0 |      60 |  
| 13 | Wish You Were Here                    | 1975 |         0 |       0 |  
| 14 | Funeral                               | 2004 |         0 |       0 |  
+----+---------------------------------------+------+-----------+---------+  

Person_Publication

+-----------+----------------+--------+---------------+  
| person_id | publication_id | editor | author_number |  
+-----------+----------------+--------+---------------+  
|        11 |             11 |      0 |             1 |  
|        12 |             11 |      0 |             1 |  
|        16 |              9 |      0 |             1 |  
|        17 |             11 |      0 |             1 |  
+-----------+----------------+--------+---------------+  

我正在尝试使用以下查询选择某个出版物的所有作者:

SELECT fName , lName 
FROM Publication , Person, Person_Publication 
WHERE Person.id = Person_Publication.person_id 
AND Person_Publication.publication_id = 11;

但我得到的结果总是重复的(由于某种原因总是 6 倍)。结果:

+-------+-----------+
| fName | lName     |
+-------+-----------+
| Paul  | McCartney |
| John  | Lennon    |
| The   | Beatles   |
| Paul  | McCartney |
| John  | Lennon    |
| The   | Beatles   |
| Paul  | McCartney |
| John  | Lennon    |
| The   | Beatles   |
| Paul  | McCartney |
| John  | Lennon    |
| The   | Beatles   |
| Paul  | McCartney |
| John  | Lennon    |
| The   | Beatles   |
| Paul  | McCartney |
| John  | Lennon    |
| The   | Beatles   |
+-------+-----------+
18 rows in set (0.03 sec)

谁能告诉我为什么会这样以及如何解决这个问题?

【问题讨论】:

    标签: mysql join many-to-many duplicates relational


    【解决方案1】:

    您将获得 6 倍的结果,每个出版物行正好一个。

    从您的 FROM 子句中删除您的出版物:

    SELECT fName , lName 
    FROM Person, Person_Publication 
    WHERE Person.id = Person_Publication.person_id 
    AND Person_Publication.publication_id = 11;
    

    【讨论】:

      【解决方案2】:

      您的查询中包含三个表:

      FROM Publication, Person, Person_Publication 
      

      但你只有一个连接条件:

      WHERE Person.id = Person_Publication.person_id 
      

      您最终得到Publication 和Person JOIN Person_Publication 之间的笛卡尔积

      将以下条件添加到您的 WHERE 块中:

      AND Publication.id = Person_Publication.publication.id
      

      为什么首选显式 JOIN 语法的完美示例。使用以下语法:

      SELECT fName, lName 
      FROM Publication
      JOIN Person_Publication ON Person_Publication.publication.id = Publication.id
      JOIN Person ON Person.id = Person_Publication.person_id
      WHERE Person_Publication.publication_id = 11;
      

      ..这样的错误根本不可能发生。

      【讨论】:

      • 谢谢,原来如此。
      • 但是由于您没有从 Publication 表中选择任何字段,因此只需将其从您的 FROM 子句中删除,正如 Alejandro Colorado 所建议的那样。
      猜你喜欢
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多