繁体   English   中英

MySQL计数不同的查询

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM