views:

69

answers:

2

Hello,

I am quite new to MySQL and it is great in reducing data through queries. However, it seems to be hard to get both records, if you want records containing duplicates.

The table looks like:

ID  Value  more columns..
1     4
1     4
1     5
2     4
3     5
3     5
4     4
4     5

I want both(!) records with duplicate values, like

 ID Value  more columns..
 1    4
 1    4
 3    5
 3    5

I need them both... as there is information in the other columns why the values are equal.

Thanks a lot!

+3  A: 

This query selects all records that have at least one other record with the same values of id and value:

SELECT  *
FROM    mytable mi
WHERE   EXISTS
        (
        SELECT  NULL
        FROM    mytable mo
        WHERE   mo.id = mi.id
                AND mo.value = mi.value
        LIMIT 1, 1
        )
Quassnoi
This queryou is genious. It took me a while to make real use of it. However, I recommend to create a temporary table of mytable so that mi is running over the tmp. One example: it reduced a query of 4400 rows from 248 second to 49 seconds... A second tmp did not work for me. Either way thanks!
+1  A: 
select *
  from mytable
 where (id, value)
    in (select * from (select id
             , value
          from mytable
        group
            by id
             , value
        having count(*) > 1))
longneck
This worked for me too, thanks. However, it was slower than the one above.
i made a small change to work around a subquery materialization bug in mysql. try it again and see if the performance is better.
longneck