1

我正在创建某种社交媒体,例如 facebook,我正在尝试使用以下查询获取帖子的点赞数。

SELECT posts.object_ID, posts.user_ID, posts.datetime, posts.text, 
    COUNT(likes.object_ID) AS likes, 
    SUM(IF(likes.user_ID=?, 1, 0)) AS allowLike, users.first_name, 
    users.last_name, userinfo.image_ID 
FROM posts 
LEFT JOIN users ON users.ID=posts.user_ID 
LEFT JOIN friends ON (friends.user_ID_1=? OR friends.user_ID_2=?) 
    AND friends.approved=1 
LEFT JOIN userinfo ON users.ID=userinfo.user_ID 
LEFT JOIN likes ON likes.object_ID=posts.object_ID 
WHERE ". $str ." 
GROUP BY posts.object_ID 
ORDER BY posts.datetime DESC 
LIMIT 0,30

的值$str只是一种过滤掉朋友和东西的机制,例如:

$str = "(posts.user_ID=friends.user_ID_1 OR posts.user_ID=friends.user_ID_2) OR posts.user_ID=? ";

现在发生的是COUNT(likes.object_ID)返回 4 并且也SUM(IF(likes.user_ID=?, 1, 0))返回 4。

令人惊讶的是,这是实际喜欢的数量乘以我拥有的朋友数量(4),我假设,因为有 2 个喜欢的帖子显示为 8 个喜欢。昨天我有 3 个朋友,结果显示为 3 个。而且只有当帖子是我自己的时才会发生。

我使用一个非常相似的查询来获取评论,但那里没有发生任何奇怪的事情。

有任何想法吗?

编辑:它不仅限于我自己的帖子。我刚刚在别人的帖子中发现了一个案例。尽管如此,它并没有发生在每个帖子上......这很奇怪。而且,它不受我个人喜好的约束......

EDIT2:在“喜欢”我在 EDIT1 中谈到的帖子后,它显示为一个,经过几次刷新后,它仍然显示为 1。奇怪的..

4

3 回答 3

0

您的问题可能与您的 WHERE 子句有关。一个参数没有被解析;您的 WHERE 只是一个长 varchar,您的 RDMS 将其解释为 TRUE。

于 2013-02-13T10:09:23.887 回答
0

好吧,我决定放弃这一点,只寻求一个类似于我自己和@Duniyadnd 在我的问题评论中描述的解决方案。

I have a table objects. It only contained an ID. I added a field 'likes'. And created two triggers: One for the newLike, and one for a deletedLike which would update the likes column at the given object_ID.

Now my query is as such:

SELECT 
    posts.object_ID, posts.user_ID, posts.datetime, posts.text, objects.likes, 
SUM(IF(likes.user_ID=?, 1, 0)) AS allowLike, users.first_name, users.last_name,   
    userinfo.image_ID 
FROM posts 
LEFT JOIN users ON users.ID=posts.user_ID 
LEFT JOIN friends ON (friends.user_ID_1=? OR friends.user_ID_2=?) AND friends.approved=1 
LEFT JOIN userinfo ON users.ID=userinfo.user_ID 
LEFT JOIN objects ON objects.ID=posts.object_ID 
LEFT JOIN likes ON likes.object_ID=posts.object_ID 
WHERE ". $str ." 
GROUP BY posts.object_ID ORDER BY posts.datetime DESC LIMIT 0,30

So I cleared my likes table to start over, yet, allowLike (SUM(IF(likes.user_ID=?, 1, 0))) still returns 4. There is only 1 entry in the likes table, I'm still curious why this is hapenning? Again, I have 4 friends.

Or would I better place this in a new question.

于 2013-02-13T10:52:38.080 回答
0

The joins are causing multiple intermediate rows to be created from the cross joins of the rows in one table with those of another table. Therefore your 4 friends are causing the 1 like to be turned into 4 intermediate rows, and then it is summed up to get a result of 4.

Instead of all the joins, try using a select within the first select, like this query on posts/comments/likes:

select postid, postmessage,
 (select count(commentid) from comment where post.postid = comment.postid) as numcomments,
 (select sum(value) from like where post.postid = like.postid) as popularity
from post
group by postid

(I have like.value as being 1 or -1 as up votes and down votes)

Hope that helps :)

于 2013-05-29T21:43:49.107 回答