1

I have this table that holds user relations of a social network website with the following structure.

+--------+----------------+
| user_id| friend_user_id |
+--------+----------------+

And there is 2 record for every individual relationship in the table. Something like the following:

+--------+----------------+
| user_id| friend_user_id |
+--------+----------------+
|    1   |        2       |
|    2   |        1       |
|    4   |        7       |
|    8   |        15      |
|    7   |        4       |
|   15   |        8       |
+--------+----------------+

Now what I need to do is for every single relationship to be only one record present. Meaning I need to delete every second record of every relationship.

I have tried various queries to no avail. If anyone could come up with the suitable query I'll be truly grateful.

2
  • How do you define which relations should be deleted ? The last ones inserted ?
    – BMN
    Nov 18, 2014 at 15:00
  • @YellowBird It doesn't really matter. As long as only one record remains for every relationship.
    – 2hamed
    Nov 18, 2014 at 15:13

1 Answer 1

3

It looks like the relationships exist in pairs. So user A has user B as a friend and user B has user A as a friend. If I read your question right, you want to remove one of the relationships but leave the other.

Since they have different user_ids, you could delete all the rows where user_id is greater than friend_user_id. That would delete one of the relationships but keep the other.

delete table where user_id > friend_user_id
2
  • Astonishingly I think you may be right. Let me get back to you on this.
    – 2hamed
    Nov 18, 2014 at 15:17
  • I think it worked. Thanks for the simple and effective answer. Cheers friend. :)
    – 2hamed
    Nov 18, 2014 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.