Table:
- activities: id, userId, description, objectId
- users: id, name
- usersFriends, id, userId, friendUserId
- objects: id, name
The current query:
SELECT A.*, U.*
FROM usersFriends UF
JOIN activities A ON A.userId = UF.friendUserId
JOIN users U ON U.id = A.userId
WHERE A.objectId = x
The problem with the current query is that if users have matching friends for example:
- user 1: tim
- user 2: ryan
- user 3: allan
- user 4: charlie
tim is friends with charlie
ryan is friends with charlie
We get the activities of object X, but we get all the activities double because tim is friends with charlie and ryan is friends with charlie, so I need some kind of distinct?
Thanks for help!
I usually do like this
... AND A.userId < UF.userId