2011-01-21 50 views
0

查詢通過瀏覽量排序的帖子 - 這完美的作品。但我也需要加入wp_term_taxonomy.term_taxonomy_id。就像這樣:WordPress的:與表加入子查詢

WHERE wp_term_taxonomy.term_taxonomy_id ='2' 

代碼:

$qstr = " 
    SELECT wposts.* 
    FROM $wpdb->posts wposts, 
    (select postid, sum(pageviews) pageviews 
    from $pageviews_table 
    group by postid) pv 
    WHERE wposts.post_status = 'publish' 
    AND wposts.post_type = 'post' 
    AND wposts.ID = pv.postid 
    ORDER BY pv.pageviews DESC 
    LIMIT 10 
"; 

我嘗試這樣做:

$qstr = " 
    SELECT wposts.*, wp_term_taxonomy.term_taxonomy_id 
    FROM $wpdb->posts wposts, 
    (select postid, sum(pageviews) pageviews 
    from $pageviews_table 
    group by postid) pv 
    WHERE wposts.post_status = 'publish' 

    INNER JOIN 
      wp_term_taxonomy 
      AND xxxx // dont know 
    WHERE wp_term_taxonomy.term_taxonomy_id = '13' 

    AND wposts.post_type = 'post' 
    AND wposts.ID = pv.postid 
    ORDER BY pv.pageviews DESC 
    LIMIT 10 
"; 

回答

0

你爲什麼要使用連接?這不適合你:

$qstr = " 
SELECT wposts.*, wp_term_taxonomy.term_taxonomy_id 
FROM $wpdb->posts wposts, 
(select postid, sum(pageviews) pageviews 
from $pageviews_table 
group by postid) pv, wp_term_taxonomy 
WHERE wposts.post_status = 'publish' 
AND wp_term_taxonomy.term_taxonomy_id = '13' 
AND wposts.post_type = 'post' 
AND wposts.ID = pv.postid 
ORDER BY pv.pageviews DESC 
LIMIT 10 "; 
+0

謝謝!這很好 – celebo 2011-01-22 11:38:41