1

I have two tables, people and comment.

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

When I execute the query:

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

I suppose it will return me only 1 row, however I'm getting a duplicate of that row:

+------------------+------------+-------------+-------------+-------------+--------------+
|      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 Answers 3

1

you joining people table twice. So you can modify your query to become:

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

also add distinct keyword if same problem occur like:

SELECT distinct comments.comment, ...
...
...
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the quick reply, however it gave me error on #1054 - Unknown column 'people.id' in 'order clause' How should i fix this?
than change it to ORDER BY creator_person.id. Also I have modify the answer
1

You should change:

FROM people, comments

to:

FROM comments

Since you are joining on the people table you do not need to include it in the FROM clause.

You will also need to update your order clause to reflect the alias name you gave the people table:

ORDER BY creator_person.id

1 Comment

thank you for the quick reply, however it gave me error on #1054 - Unknown column 'people.id' in 'order clause' How should i fix this?
0

You have an extra join in there (an implicit one in people,comments since you are after doing an inner join with people you should remove the first).

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

I also moved the other validations from the on clause to the where clause. In this case, you are doing a inner join, it wouldn't make a difference, but this is a bad habit and could make you do weird things when you use left/right joins in the future

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.