1

我有三张桌子:

+--------+-----------+  
| 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)

有人可以告诉我为什么会发生这种情况以及如何解决这个问题吗?

4

2 回答 2

2

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

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

SELECT fName , lName 
FROM Person, Person_Publication 
WHERE Person.id = Person_Publication.person_id 
AND Person_Publication.publication_id = 11;
于 2013-06-23T00:53:35.940 回答
1

您在查询中包含三个表:

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;

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

于 2013-06-23T00:54:50.073 回答