If there is a symmetry relationship in a table, how to represent it in a elegant way?
For example, there is a table called Friend
, in which should contain user ID of two users. If we use UID1
and UID2
in this table, when we want to find out if A_uid and B_uid are friends, we should use
SELECT * FROM Friend WHERE (UID1 = A_uid AND UID2 = B_uid) OR (UID1 = B_uid AND UID2 = A_uid);
since UID1
and UID2
are the same in representing a friendship. And the most important in this dilemma is that UID1
and UID2
are symmetry.
I think this is ugly and want to know if there is a better way to do with it.
Thanks!