显示重复行的MySQL
我有两个表,显示重复行的MySQL,mysql,Mysql,我有两个表,人和评论 Table: people +-------------------+----------------+-------------+------+ | id | cn | en | dob | role | +-------------------+----------------+-------------+------+ | 1 | ChineseName | EnglishName | 1989-
人
和评论
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
我想它只会返回1行,但是我得到了该行的一个副本:
+------------------+------------+-------------+-------------+-------------+--------------+
| 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 |
+------------------+------------+-------------+-------------+-------------+--------------+
您应该更改:
FROM people, comments
致:
由于您加入的是people
表,因此不需要将其包含在FROM
子句中
您还需要更新order子句以反映您在people表中提供的别名:
ORDER BY creator_person.id
您可以加入
人员
表两次。因此,您可以将查询修改为:
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, ...
...
...
你在那里有一个额外的连接(在
人物,评论中有一个隐式连接,因为你在与人进行内部连接之后,应该首先删除该连接)
我还将其他验证从on子句移动到where子句。在这种情况下,您正在进行内部联接,这不会有什么区别,但这是一个坏习惯,可能会使您在将来使用左/右联接时做一些奇怪的事情谢谢您的快速回复,然而,它在“order子句”中的#1054-未知列“people.id”上给了我错误
我应该如何解决这个问题?然后将其更改为order BY creator_person.id
。另外,我已经修改了答案谢谢你的快速回复,但是它在“订单条款”中的#1054-未知列“people.id”上给了我错误。
我应该如何解决这个问题?
SELECT distinct comments.comment, ...
...
...
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;