First of all, you are missing the GROUP BY clause in your query.
Second, I would use an INNER JOIN between the users table and the relationships table (there should never be userid's in the relationship table that don't exist in the users table).
And once you use an INNER JOIN between the users table and the relationships table, the join with the photos table should be on the user id from the users table (more logical, and also gets rid of the OR in that join).
About the wrong count result:
You are multiplying the count result because each user has more than 1 photo. So you could extract only the avatar (presuming each user has max 1 avatar):
Code:
SELECT
Relationship.ID
, Relationship.ID1
, Relationship.ID2
, Relationship.Relationship
, COUNT (Relationship.Relationship) as friend
, User.ID as UserID
, User.Name
, Photo.Path
, Photo.Avatar
FROM Relationship
INNER JOIN User
ON (Relationship.ID1 = User.ID OR Relationship.ID2 = User.ID)
LEFT JOIN Photo
ON User.ID = Photo.User_id
AND Photo.Avatar = 1
WHERE (Relationship.ID1 = '$user_id' OR Relationship.ID2 = '$user_id')
AND Relationship = 'friend'
GROUP BY Relationship.ID
ORDER BY Relationship.ID
LIMIT 5
Or if you want all the photo's, you'll have to count the number of relations in a subquery, and then join the photo table:
Code:
SELECT
rels.ID
, rels.ID1
, rels.ID2
, rels.Relationship
, rels.friend
, rels.UserID
, User.Name
, Photo.Path
, Photo.Avatar
FROM
(SELECT
Relationship.ID
, Relationship.ID1
, Relationship.ID2
, Relationship.Relationship
, COUNT (Relationship.Relationship) as friend
, User.ID as UserID
, User.Name
FROM Relationship
INNER JOIN User
ON (Relationship.ID1 = User.ID OR Relationship.ID2 = User.ID)
WHERE (Relationship.ID1 = '$user_id' OR Relationship.ID2 = '$user_id')
AND Relationship = 'friend'
GROUP BY Relationship.ID
) as rels
LEFT JOIN Photo
ON User.ID = Photo.User_id
ORDER BY Relationship.ID
LIMIT 5
Bookmarks