mysql 相同id,MYSQL-获取具有相同ID的多个记录的所有记录

I apologize in advanced if I am not explaining this correctly. I can barely explain it in english terms, let alone in a mysql query.

I am trying to get the list of response_set_ids that have more than 1 record for a question_id.

This is an example of my data:

+----+-----------------+-------------+-----------+

| id | response_set_id | question_id | answer_id |

+----+-----------------+-------------+-----------+

| 1 | 10 | 1 | 4 |

| 2 | 10 | 2 | 5 |

| 3 | 10 | 3 | 6 |

| 4 | 10 | 3 | 7 |

| 5 | 11 | 1 | 8 |

| 6 | 11 | 2 | 9 |

| 7 | 11 | 3 | 10 |

+----+-----------------+-------------+-----------+

I would like to have a query that would return me a list response_set_ids, and in this particular example, I would expect to get returned 10 because that response_set has question_id -> 3 showing up more than once.

Please let me know if you need any further information to help me.

I have tried this:

select response_set_id, count(question_id) from responses group by response_set_id;

But that only gives me the counts of questions per response_set.

Thank you in advanced!

解决方案

The simplest method doesn't use a subquery:

SELECT DISTINCT response_set_id

FROM responses

GROUP BY response_set_id, question_id

HAVING COUNT(*) > 1;

This is one of the very, very few instances where select distinct is used (appropriately) with group by.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值