I have a simple table (MySQL) set up for relationships between two users.
[User1Id] [User2Id]
10 15
14 10
10 13
But, I can't figure out how to do a SELECT in such a way that I can grab the opposite UserId of the user being requested. So, if I wanted to get all relations for the user with an ID of 10, it would grab the user IDs 15, 14, and 13, and ideally join the users table with those IDs to get those users usernames. My current try 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 that gets filtered and rearranged in code. Is there a way that I could do one SQL query to grab everything that I need?