5

所以我有一个查询试图抓取“相关帖子”。

类别与帖子具有一对多的关系。标签具有多对多的关系。所以我的表大致是这样的:

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

绑定:初始帖子类别 ID;初始帖子标签 ID;

显然,这实际上不会按子选择中的正确值对结果进行排序。我在尝试考虑如何加入这一点以获得正确的结果时遇到了麻烦。

提前致谢!

4

2 回答 2

3

试试这个,

SELECT posts.* 
FROM   posts,(SELECT p.id, 
                     Count(pt.tag_id) AS count_tag 
              FROM   posts AS p, 
                     post_tag AS pt 
              WHERE  p.category_id = '***' 
                     AND pt.post_id = p.id 
                     AND pt.tag_id IN(SELECT tag_id 
                                      FROM   post_tag 
                                      WHERE  post_tag.post_id = '***') 
              GROUP  BY p.id 
              ) temp

WHERE  posts.id =temp.id ORDER  BY temp.count_tag desc

您可以在哪里填写***,因为您已经有 1 个帖子行

于 2012-10-06T17:56:44.823 回答
3

如果我正确地理解了您的问题,这就是您要寻找的:

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 

绑定:初始posts.id

并且您只需在我的版本中指定当前帖子的 id,这样您就不必事先获取帖子标签并为 in 子句适当地格式化它们

编辑:通过避免双重加入帖子,这应该是一个更快的查询,如果您不喜欢用户变量,只需将所有 currentpostid 替换为 ? 和三重绑定 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;
于 2012-10-06T18:05:42.900 回答