TABLE user
userId username
14 john
2 sally
3 mike
Table friend
userId friendId
14 2
3 14
3 2
I would like to query for the userId and username of all of john’s friends. I attempted to use this query:
(
SELECT u.username, u.userId
FROM user AS u, friend AS f
WHERE f.userId = 14
AND f.friendId = u.userId
)
UNION
(
SELECT u.username, u.userId
FROM user AS u, friend AS f
WHERE f.friendId = 14
AND f.userId = u.userId
)
But this only returned 2(sally). It should be returning 2(sally) and 3(mike). Is the problem with the second clause? I tried by it itself and it returned an empty result.