MySQL显示重复的行

时间:2013-11-28 01:14:18

标签: mysql

我有两个表,peoplecomment

Table: people
+-------------------+----------------+-------------+------+
| id |      cn      |       en       |     dob     | role |
+-------------------+----------------+-------------+------+
| 1  |  ChineseName |   EnglishName  |  1989-03-02 |   0  |
+-------------------+----------------+-------------+------+
| 2  | ChineseName2 |   EnglishName2 |  1923-06-12 |   1  |
+-------------------+----------------+-------------+------+

Table: comment
+----+--------+----------+-------------------+---------------------+
| id |  owner | owner_id | creator_person_id |       comment       |
+----+--------+----------+-------------------+---------------------+
| 1  | PERSON |     2    |          1        |  Some comments here |
+----+--------+----------+-------------------+---------------------+
| 2  | TRANSAC|     1    |          1        |       Comments      |
+----+--------+----------+-------------------+---------------------+
| 3  | PERSON |     1    |          1        |     Comments here   |
+----+--------+----------+-------------------+---------------------+

执行查询时:

SELECT comments.comment, 
creator_person.id AS creator_id,
creator_person.cn AS creator_cn,
creator_person.en AS creator_en,
creator_person.dob AS creator_dob,
creator_person.role AS creator_role
FROM people, comments
JOIN people AS creator_person
ON comments.creator_person_id = creator_person.id AND comments.owner = 'PERSON' AND comments.owner_id = 1
ORDER BY people.id

我想它只会给我一行,但是我得到了那一行的副本:

+------------------+------------+-------------+-------------+-------------+--------------+
|      comment     | creator_id | creator_cn  | creator_en  | creator_dob | creator_role |
+------------------+------------+-------------+-------------+-------------+--------------+
|   Comments here  |      1     | ChineseName | EnglishName |  1989-03-02 |       0      |
+------------------+------------+-------------+-------------+-------------+--------------+
|   Comments here  |      1     | ChineseName | EnglishName |  1989-03-02 |       0      |
+------------------+------------+-------------+-------------+-------------+--------------+

3 个答案:

答案 0 :(得分:1)

你应该改变:

FROM people, comments

为:

FROM comments

由于您要加入people表,因此无需将其包含在FROM子句中。

您还需要更新订单子句以反映您为人员表提供的别名:

ORDER BY creator_person.id

答案 1 :(得分:1)

你加入people表两次。因此,您可以将查询修改为:

SELECT comments.comment, 
creator_person.id AS creator_id,
creator_person.cn AS creator_cn,
creator_person.en AS creator_en,
creator_person.dob AS creator_dob,
creator_person.role AS creator_role
FROM comments
INNER JOIN people AS creator_person
ON comments.creator_person_id = creator_person.id 
   AND comments.owner = 'PERSON' AND comments.owner_id = 1
ORDER BY creator_person.id

如果出现同样的问题,也会添加distinct关键字:

SELECT distinct comments.comment, ...
...
...

答案 2 :(得分:0)

你有一个额外的连接(people,comments中的隐式连接,因为你在与你应该删除第一个的人进行内连接之后)。

select comments.comment,
  creator_person.id as creator_id,
  creator_person.cn as creator_cn,
  creator_person.en as creator_en,
  creator_person.dob as creator_dob,
  creator_person.role as creator_role
from comments
inner join people as creator_person
  on comments.creator_person_id = creator_person.id
where comments.owner = 'PERSON' and comments.owner_id = 1
order by creator_person.id;

sqlfiddle demo

我还将其他验证从on子句移到了where子句。在这种情况下,你正在做一个内部联接,它不会有所作为,但这是一个坏习惯,当你在将来使用左/右连接时可能会让你做奇怪的事情