SELECT postID, threadID, COUNT(DISTINCT postID) as count
FROM wbb1_1_post post
LEFT JOIN wcf1_user_to_groups groups
ON post.userID = groups.userID
WHERE threadID IN (468)
AND groupID IN (4,10)
AND isDisabled != 1
AND isDeleted != 1
ORDER BY postID ASC
克里斯 = groupID 4
塞巴斯蒂安 S. = groupID 10
结果:
postID threadID count
2811 468 2
一切都是正确的。
SELECT postID, threadID, COUNT(DISTINCT postID) as count
FROM wbb1_1_post post
LEFT JOIN wcf1_user_to_groups groups
ON post.userID = groups.userID
WHERE threadID IN (68)
AND groupID IN (7)
AND isDisabled != 1
AND isDeleted != 1
ORDER BY postID ASC
talishh 和 Bender = groupID 7
结果:
postID threadID count
349 (wrong!) 68 3
结果应该是 308,因为我想要最旧的帖子(这就是我按 postID ASC 排序的原因)。 有没有办法优化这个查询?