2009-05-04 74 views

回答

0

怎麼樣...:

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)表中的兩個外鍵?

5

當然,你可以做到在一個查詢:

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; 
相關問題