Hi, I have these tables
Table Relationship
ID ID1 ID2 Relationship
1 1 2 friend
2 3 4 family
Table User
Id Name
1 John
2 Jim
Table Photo
Photo_ID User_id Path Avatar
1 1 path_to_photo 0
2 1 path_to_photo 0
3 1 path_to_photo 0
4 1 path_to_photo 1
5 2 path_to_photo 1
6 2 path_to_photo 0
7 1 path_to_photo 0
This is my codes and it’s not effective
SELECT Relationship.ID, Relationship.ID1, Relationship.ID2, Relationship.Relationship, COUNT (Relationship.Relationship) as friend, User.ID, User.Name, Photo.User_id, Photo.Path, Photo.Avatar
FROM Relationship
LEFT JOIN User ON (Relationship.ID1 = User.ID OR Relationship.ID2 = User.ID)
LEFT JOIN Photo ON (Relationship.ID1 = Photo.User_id OR Relationship.ID2 = Photo.User_id)
WHERE (Relationship.ID1 = '$user_id' OR Relationship.ID2 = '$user_id') AND Relationship = 'friend'
ORDER BY Relationship.ID
LIMIT 5
My PHP code:
if($row['friend'] >= 1) {
if($row['Avatar'] == '1') {
show this picture
break;
} else{
show default picture
}
}else{
show default picture
}
I put count result is the most important thing, because it would count unchecked activities between the two people. The code above generate multiple counts. In a real life practice, if I join 2 tables the count is 3 and if join 3 tables it become 12.
Thanks