[英]MySQL count distinct query
我有一張桌子,我一直在張貼評論類型,金額等信息。
PostExtras
- id
- amount
- post_id (foreign key)
- comment_type (foreign key)
- ...
comment_type
- id
- name
我想選擇評論類型重復的帖子。
例:
- 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
第1和第5行實際上是相同的。
如果我理解正確,請使用COUNT,GROUP BY和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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.