2010-11-07 205 views
1

該查詢獲取與其他新聞文章類似標記的新聞文章。是否有可能消除我沒有看到的這些子查詢之一?幫助優化子查詢

SELECT id, 
     title, 
     comment_count 
    FROM NEWS 
WHERE (franchise_id = 1 OR franchise_id = 0) 
    AND draft = 0 
    AND id != 459 
    AND id IN (SELECT news_id 
       FROM news_tag_association 
       WHERE tag_id IN (SELECT tag_id 
            FROM news_tag_association 
           WHERE news_id = 459))        
LIMIT 0, 5; 

回答

2

是的,沒有GROUP BY或類似的,所以你可以使用正常的自聯接:

SELECT 
    id, title, comment_count 
FROM news 
JOIN news_tag_association n2t1 ON (news.id = n2t1.news_id) 
JOIN news_tag_association n2t2 ON (n2t1.tag_id = n2t2.tag_id) 
WHERE franchise_id IN (0, 1) 
    AND draft = 0 
    AND id != 459 
    AND n2t2.news_id = 459 
LIMIT 0,5;