Skip to content Skip to sidebar Skip to footer

Mysql - Get All Records That Have More Than 1 Record For The Same 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

Solution 1:

The simplest method doesn't use a subquery:

SELECTDISTINCT response_set_id
FROM responses
GROUPBY response_set_id, question_id
HAVINGCOUNT(*) >1;

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

Solution 2:

selectdistinct response_set_id from (
    select  response_set_id , question_id  
    from 
    responses
    groupby 
    response_set_id, question_id
    havingcount(*)>1) a

Solution 3:

I believe this question has been asked before but I cannot add comments at my current rep:

SELECTDISTINCT response_set_id
FROM responses
GROUPBY question_id
HAVINGCOUNT(question_id) >1

Post a Comment for "Mysql - Get All Records That Have More Than 1 Record For The Same Id"