I am trying to execute a query to find all users that are not 'friends' of the current user. For example, if the user is logged in as 'alex', I want to display all users in the users
table that are not friends of alex (i.e. david).
I'm using PHP Codeigniter if that helps. This is the query I came up with (but it returns nothing):
public function getUser($username) {
$this->db->query("SELECT username FROM users WHERE username NOT IN
(SELECT user1 FROM friendships WHERE user2 = '$username'
UNION SELECT user2 FROM friendships WHERE user1 = '$username')");
return $query->result_array();
}
MySQL Tables
Users
+----+----------+
| id | username |
+----+----------+
| 1 | alex |
+----+----------+
| 2 | james |
+----+----------+
| 3 | david |
+----+----------+
Friendships
+----+-------+-------+
| id | user1 | user2 |
+----+-------+-------+
| 1 | alex | james |
+----+-------+-------+
| 2 | james | david |
+----+-------+-------+