我有下表(示例):
用户:
id | user | photo | joined | country
1 | Igor | abc.jpg | 2015 | Brazil
2 | John | cga.png | 2014 | USA
3 | Lucas| hes.jpg | 2016 | Japan
posts(看到有两行 author = Igor
和 ft = 2
和一行 author = Igor
和 ft = 3
和 Igor 有三个帖子):
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
友谊(当status = 2
表示他们是 friend 时):
id | friend1 | friend2 | status
1 | Igor | Lucas | 2
2 | Lucas | John | 2
3 | John | Igor | 2
我想用 ft = 2
和 COUNT
friend ( status = 2
) 根据当前登录的用户(在本例中为 Igor)。
所以,我这样做(假设当前登录的用户是 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
当我检查 foreach
时,数据是正确的:
numPhotos = 2
和 numVideos = 1
。
但是,我也想选择 friend 的数量,所以,我这样做:
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
但是,输出是:
numPhotos = 4
、numVideos = 2
和 numFriends = 6
。
换句话说,他复制了所有结果,但在 numFriends
中,他获取了 Igor (3) 的帖子总数并复制了值。如果我将 count(friendship.status)
更改为 sum(friendship.status)
,则输出为:
numPhotos = 4
、numVideos = 2
和 numFriends = 18
(numFriends
的三倍)。
我也尝试过 count(distinct friendship.status)
结果是:
numPhotos = 4
、numVideos = 2
和 numFriends = 1
(再次复制值并为 numFriends 返回错误值 1
应该是 2 知道他有两个 friend )。
那么,我该怎么做呢? (我正在使用 MySQL)
编辑:
我将 count(distinct friendship.status)
更改为 count(distinct friendship.id)
并且它可以选择 friend 的数量。但其余值(numPhotos
和 numVideos
)继续重复。
我发现问题出在 ON (users.user = friend1 OR users.user = friend2)
中,因为如果我只保留 ON (users.user = friend1)
或 ON (users.user = friend2)
输出不重复。我也尝试过 ON 'Igor' IN (friend1, friend2) 但结果是一样的(
numPhotosand
numVideos` continue duplicated)。
最佳答案
我认为左联接可能是在一对多关系上联接,这会导致计数膨胀。 由于您只检索 1 个用户的计数,我建议使用子查询来检索友谊计数(对于检索多个用户的计数,派生表可能比子查询更快):
SELECT
sum(ft = 2) AS numPhotos,
sum(ft = 3) AS numVideos,
(select count(*) from friendships f
where (friend1 = users.user
or friend2 = users.user)
and status = 2) as friendship_count
FROM users
LEFT JOIN posts
ON users.user = posts.author
WHERE users.user = 'Igor'
请注意,我删除了 group by
,因为 users.user
已经在 where 子句中,这意味着只有 1 个组。
关于php - LEFT JOIN 中的 COUNT 返回重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330912/