WordPress:带表连接的子查询

时间:2011-01-21 22:59:20

标签: mysql wordpress

查询按网页浏览量对帖子进行排序 - 这非常有效。但我也需要加入 wp_term_taxonomy.term_taxonomy_id 。像这样:

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
 ";

1 个答案:

答案 0 :(得分: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 ";