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)