[Solved] Avoid data redundancy in a friends table

Add field friendship_key: ALTER TABLE t_friend ADD friendship_key decimal(22,11); CREATE UNIQUE INDEX friendship_key_unique ON t_friend (friendship_key); And php part: $friends = [$userId, $friendId]; $key = min($friends).’.’.max($friends); $q = “SELECT * FROM t_friend WHERE friendship_key = “.$key; insert: $friends = [$userId, $friendId]; $key = min($friends).’.’.max($friends); $q = “INSERT INTO t_friend (friendship_key, userId, friendId) VALUES (“.implode(‘,’, [$key, … Read more