I've the following tables (example):
users:
id | user | photo | joined | country
1 | Igor | abc.jpg | 2015 | Brazil
2 | John | cga.png | 2014 | USA
3 | Lucas| hes.jpg | 2016 | Japan
posts (see that there are two lines with author = Igor
and ft = 2
and one line with author = Igor
and ft = 3
and Igor have three posts):
id | author | content | date | ft (2 = photos and 3 = videos)
1 | Igor | hi | 2016 | 2
2 | Igor | hello | 2016 | 3
3 | John | hehehe | 2016 | 2
4 | Igor | huhuhuh | 2016 | 2
5 | Lucas | lol | 2016 | 3
friendship (when status = 2
means that they are friends):
id | friend1 | friend2 | status
1 | Igor | Lucas | 2
2 | Lucas | John | 2
3 | John | Igor | 2
And I want to do a COUNT
of posts with ft = 2
and a COUNT
of friends (status = 2
) according to the currently logged user (Igor, in this case).
So, I do (assuming that the current user logged in is Igor):
SELECT photo, joined, country, sum(CASE WHEN ft = 2 THEN 1 ELSE 0 END) AS numPhotos, sum(CASE WHEN ft = 3 THEN 1 ELSE 0 END) AS numVideos
FROM users
LEFT JOIN posts
ON users.user = posts.author
WHERE users.user = 'Igor'
GROUP BY users.user
LIMIT 1
And when I check on a foreach
, the data is correct:
numPhotos = 2
and numVideos = 1
.
But, I want to select too the number of friends, so, I do:
SELECT photo, joined, country, sum(CASE WHEN ft = 2 THEN 1 ELSE 0 END) AS numPhotos, sum(CASE WHEN ft = 3 THEN 1 ELSE 0 END) AS numVideos, count(friendship.status) AS numFriends
FROM users
LEFT JOIN posts
ON users.user = posts.author
LEFT JOIN friendship
ON (users.user = friend1 OR users.user = friend2) AND friendship.status = 2
WHERE users.user = 'Igor'
GROUP BY users.user
LIMIT 1
But, the output is:
numPhotos = 4
, numVideos = 2
and numFriends = 6
.
In other words, he is duplicating all results but in numFriends
he's taking the total of posts of Igor (3) and duplicating the value too. And if I change count(friendship.status)
to sum(friendship.status)
the output is:
numPhotos = 4
, numVideos = 2
and numFriends = 18
(triples the numFriends
).
I tried too with count(distinct friendship.status)
and the result is:
numPhotos = 4
, numVideos = 2
and numFriends = 1
(duplicates the values again as well as return the wrong value 1 for numFriends
that should be 2 knowing he has two friends).
So, how I can do this? (I'm using MySQL)
EDIT:
I changed the count(distinct friendship.status)
to count(distinct friendship.id)
and it worked to select the number of friends. But the rest of values (numPhotos
and numVideos
) continue duplicated.
I discovered that the problem is in ON (users.user = friend1 OR users.user = friend2)
, because if I leave only ON (users.user = friend1)
or ON (users.user = friend2)
the output isn't duplicated. I tried too with ON 'Igor' IN (friend1, friend2) but the result is the same (
numPhotosand
numVideos` continue duplicated).