1
我想顯示相關博客帖子的列表,我希望列表按照它們對當前帖子具有的通用標籤的數量進行排序。每篇文章可以有多個與之關聯的標籤。這裏是我的表結構:如何列出按常用標籤數排序的相關博客文章?
[文章] < - [帖子到標籤接合用表] - > [標籤]
我使用PHP和MySQL - 我能做到這一點一個查詢?
我想顯示相關博客帖子的列表,我希望列表按照它們對當前帖子具有的通用標籤的數量進行排序。每篇文章可以有多個與之關聯的標籤。這裏是我的表結構:如何列出按常用標籤數排序的相關博客文章?
[文章] < - [帖子到標籤接合用表] - > [標籤]
我使用PHP和MySQL - 我能做到這一點一個查詢?
怎麼樣...:
SELECT COUNT(*) AS numcommon, posts.pid, posts.post FROM posts
INNER JOIN p2t ON p2t.pid = posts.pid
WHERE p2t.tid IN
(SELECT p2t.tid FROM p2t
INNER JOIN posts ON p2t.pid = posts.pid
WHERE posts.pid = 1)
AND posts.pid != 1
GROUP BY posts.pid
ORDER BY numcommon
假設PID作爲職位表的主鍵,TID作爲主鍵標籤表,p2t(post to tag)表中的兩個外鍵?
當然,你可以做到在一個查詢:
SELECT postid, count(tagid) as common_tag_count
FROM posts_to_tags
WHERE tagid IN (SELECT tagid FROM posts_to_tags WHERE postid = 2)
GROUP BY postid ORDER BY common_tag_count DESC;