I have a MYSQL
database where I would like to compare duplicated values based on two values in a column. For Example
Database
field_one | field_two | field_three ------------------------------------ aaa | 123 | no1 bbb | 456 | no1 aaa | 123 | no2 ccc | 456 | no3 aaa | 123 | no3
I would like to return these results
field_one | field_two | field_three ------------------------------------ aaa | 123 | no1 aaa | 123 | no2
Here is the query
I've been using but I'm not sure how to get my desired result. When I run the query
in phpMyAdmin
my browser just gets stuck. My database is also large too.
SELECT * FROM
table_name
WHERE field_three
IN (SELECT field_one
, field_two
FROM table_name
WHERE field_three
IN ('no1', 'no2') HAVING COUNT(*) > 1)
Thanks
SOLVED
I just changed WHERE
@Gordon Linoff 's from query
.
select t.*
from table_name t join
(select field_one, field_two
from table_name t
group by field_one, field_two
having count(*) = 2 -- or do you mean >= 2?
) tsum
on t.field_one = tsum.field_one and t.field_two = tsum.field_two WHERE field3 IN ('no1', 'no2')
I think you want this:
select t.*
from t join
(select field_one, field_two
from t
group by field_one, field_two
having count(*) = 2 -- or do you mean >= 2?
) tsum
on t.field_one = tsum.field_one and t.field_two = tsum.field_two
The subquery finds the duplicates based on the first two columns. The outer query returns the original rows.
If you want duplicates relative to another value, then add that condition to the subquery:
select t.*
from t join
(select field_one, field_two
from t
where field3 = 'no1' -- or whatever you want to set it to
group by field_one, field_two
having count(*) = 2 -- or do you mean >= 2?
) tsum
on t.field_one = tsum.field_one and t.field_two = tsum.field_two