Search code examples
mysqlselectdistinct-values

MySQL count distinct query


I have a table which i kept kept posts comment_type, amount etc.

PostExtras
- id
- amount
- post_id (foreign key)
- comment_type (foreign key)
- ...

comment_type
- id
- name

I want to select posts which have duplicates comment-type.

Example:

- id     - amount    - post_id    - comment_type
 1         20          23           1
 2         45          23           2
 3         80          28           1
 4         78          28           2
 5         56          23           1

row 1 and 5 is actually same.


Solution

  • SELECT post_id FROM PostExtras
    GROUP BY post_id, comment_type
    HAVING COUNT(comment_type) > 1