所以我有一个试图获取“相关帖子”的查询。
类别与帖子具有一对多关系。标签具有多对多关系。所以我的表格大致如下所示:
posts table:
id | category_id | ... | ...
tags table:
id | ... | ...
post_tag intermediate table:
id | post_id | tag_id | ... | ...
因此,如果我已经有一个单独的帖子行,以及如何获取其“相关”帖子。我的逻辑大致是我只想抓取同一类别的帖子,但要按照与原始帖子匹配的标签数量对这些帖子进行排序。因此,与原始帖子具有完全相同标签的同一类别中的另一篇帖子应该是一个非常高的匹配,而仅匹配 3/4 标签的帖子将在结果中显示较低。
这是我目前所拥有的:
SELECT *
FROM posts AS p
WHERE p.category_id=?
ORDER BY ( SELECT COUNT(id)
FROM post_tag AS i
WHERE i.tag_id IN( ? )
)
LIMIT 5
绑定(bind): 初始帖子类别 ID; 初始帖子标签 ID;
显然,这实际上不会根据子选择中的正确值对结果进行排序。我在想如何加入这个来获得正确的结果时遇到了麻烦。
提前致谢!
最佳答案
如果我正确理解了你的问题,这就是你要找的:
SELECT p.*,
Count(pt.tag_id) AS ord
FROM posts AS currentpost
JOIN posts AS p
ON p.category_id = currentpost.category_id
AND p.id != currentpost.id
JOIN post_tag AS pt
ON pt.post_id = p.id
AND pt.tag_id IN (SELECT tag_id
FROM post_tag
WHERE post_id = currentpost.id)
WHERE currentpost.id = ?
GROUP BY p.id
ORDER BY ord DESC
绑定(bind):初始posts.id
;
而且你只需要在我的版本中指定当前帖子的 id,这样你就不必事先获取帖子标签并为 in 子句适本地格式化它们
编辑: 如果您不喜欢用户变量,只需将所有 currentpostid 替换为 ?和三重绑定(bind) post_id:
set @currentpostid = ?;
select p.*, count(pt.tag_id) as ord
from posts as p,
join post_tag as pt
on pt.post_id = p.id
and pt.tag_id in (select tag_id from post_tag where post_id = @currentpostid)
where p.category_id = (select category_id from posts where id=@currentpostid)
and p.id != @currentpostid
group by p.id
order by ord desc;
关于MySQL 按中间表中的匹配数排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762073/