If I have a table with important 2 columns,
CREATE TABLE foo (id INT, a INT, b INT, KEY a, KEY b);
How can I find all the rows that have both a
and b
being the same in both rows? For example, in this data set
id | a | b
----------
1 | 1 | 2
2 | 5 | 42
3 | 1 | 42
4 | 1 | 2
5 | 1 | 2
6 | 1 | 42
I want to get back all rows except for id=2
since it is unique in (a,b)
. Basically, I want to find all offending rows that would stop a
ALTER TABLE foo ADD UNIQUE (a, b);
Something better than an n^2 for loop would be nice since my table has 10M rows.
For bonus points : How do I removed all but one of the rows (I don't care which ones, as long as one is left)
SELECT *
FROM foo first
JOIN foo second
ON ( first.a = second.a
AND first.b = second.b )
AND (first.id <> second.id )
Should come up with all the rows where more that one row has the same combination of a and b.
Just hope you have an index on columns a and b.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With