I have a simple table (MySQL) configured for a relationship between two users.
[User1Id] [User2Id]
10 15
14 10
10 13
But I canβt figure out how to make SELECT so that I can capture the opposite UserId of the requested user. So, if I wanted to get all the relationships for the user with identifier 10, he would capture user IDs 15, 14 and 13 and ideally go into the user table with these identifiers to get these user names. My current attempt is less than ideal:
SELECT u1.username AS U1Username,
u2.username AS U2Username,
u1.userId AS U1UserId,
u2.userId AS U2UserId
FROM buddies b
LEFT JOIN users u1 on u1.userId=b.user2Id
LEFT JOIN users u2 on u2.userId=b.user1Id
WHERE b.user1Id=:1 OR b.user2Id=:1
Then it is filtered and reordered in the code. Is there a way that I could make one SQL query to capture everything I need?