开发者

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     开发者_StackOverflow      1
 4         78          28           2
 5         56          23           1

row 1 and 5 is actually same.


If I understand it right, use COUNT, GROUP BY and HAVING

SELECT *, COUNT(*) AS itemcount FROM PostExtras 
GROUP BY post_id, comment_type
HAVING itemcount >= 2


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜