MySQL 查询不应该返回重复项

标签 mysql join many-to-many duplicates relational

我有三个表:

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

有人可以告诉我为什么会这样以及如何解决这个问题吗?

最佳答案

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

从 FROM 子句中删除您的 Publication:

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

关于MySQL 查询不应该返回重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256735/

相关文章:

MySQL-使用基于另一个表的值填充表字段

php - 在 codeigniter 中连接来自多个表的数据

mysql - Spring boot - Hibernate JPA 不创建 MySql DB 表

php - 如何在不重新加载页面的情况下提交表单

SQL Server 查询 - 如果不匹配则返回空值

many-to-many - django-filters BaseInFilter 和 CharFilter 在 django-rest-framework 为空时不返回任何结果

mysql - table 太多; MySQL 在一个连接中只能使用 61 个表

mysql - 如果 IN TableB.Code,则从 TableA.Code 中选择

php - 如何过滤mysql内连接查询的结果

ruby-on-rails - 多对多用户和组,但组有所有者