The problem arose because the relationships you are trying to describe are symmetrical, but the circuit models an asymmetric relationship. The right to model a problem would be to maintain a relationship table - then there is a table linking users to relationships, for example.
relationship:
id auto_increment
related:
r_id foreign key references relationship.id
u_id foreign key references user.id
primary key (r_id, u_id)
But to clean up existing data ... an obvious approach would be ...
DELETE FROM yourtable d
WHERE A>B AND EXISTS (
SELECT 1
FROM yourtable r
WHERE r.A=d.B
AND r.B =d.A
)
However, if I correctly recall that MySQL does not like to use a subselect in deletion that refers to the same table as delete. So....
SELECT d.A,d.B
INTO dups
FROM yourtable d, yourtable r
WHERE d.A>d.B
AND r.A=d.B
AND r.B =d.A;
then ....
DELETE FROM yourtable
WHERE EXISTS (
SELECT 1 FROM dups
WHERE dups.A=yourtable.A
AND dups.B=yourtable.B
)
Not sure if the pushed predicate will cause the problem anyway, so if that doesn't work ...
DELETE FROM yourtable
WHERE CONCAT(A, '/', B) IN (
SELECT CONCAT(A, '/' B) FROM dups
)
source
share