操纵预先编写的MySQL查询 - PHP

时间:2011-03-08 04:06:55

标签: php mysql

我有以下查询返回wordpress的8个帖子ID的列表,我想接下来那些8个ID并将它们与wp_posts表中的id匹配并得到*详细信息,这样我就可以回复帖子标题和所有这些,有一种方法可以在一个查询中执行此操作吗? ..这就是我现在拥有的

SELECT postid FROM wp_popularpostsdata WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day 
AND wp_popularpostsdata.postid NOT IN (
SELECT object_id
FROM wp_term_relationships AS r
    JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
    JOIN wp_terms AS t ON t.term_id = x.term_id
WHERE   x.taxonomy = 'category'
AND t.term_id IN (3,1)) 
GROUP BY postid                                     
ORDER BY pageviews DESC LIMIT 8;

Cheeers,

3 个答案:

答案 0 :(得分:1)

您是否尝试过加入表格?

SELECT postid, wp_posts.* FROM wp_popularpostsdata 
LEFT JOIN wp_posts ON wp_posts.id = wp_popularpostsdata.postid 
WHERE 
   DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day AND 
   wp_popularpostsdata.postid NOT IN (
      SELECT object_id
      FROM wp_term_relationships AS r
      JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
      JOIN wp_terms AS t ON t.term_id = x.term_id
      WHERE   
       x.taxonomy = 'category' AND 
       t.term_id IN (3,1)) 
GROUP BY postid                                     
ORDER BY pageviews DESC LIMIT 8;

wp_posts.id替换为wp_posts

中的关键字段

答案 1 :(得分:0)

或者您可以通过另一个子选择来获取wp_posts数据。

SELECT * FROM wp_posts WHERE id IN (
  SELECT postid FROM wp_popularpostsdata 
  WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day 
  AND wp_popularpostsdata.postid 
  NOT IN (
    SELECT object_id
    FROM wp_term_relationships AS r
        JOIN wp_term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
        JOIN wp_terms AS t ON t.term_id = x.term_id
    WHERE   x.taxonomy = 'category'
    AND t.term_id IN (3,1)
      ) 
  GROUP BY postid                                     
  ORDER BY pageviews DESC LIMIT 8;
)

答案 2 :(得分:0)

你应该把id的数组('155','312','18','22',...)放在

$posts = new WP_Query(array(
    'post__in'  => array( '155', '312', '18','22',.. ) /* YOUR ARRAY */
));

它将为您提供已应用过滤器的帖子。有关http://codex.wordpress.org/Function_Reference/WP_Query

上的WP_query的更多信息

&GT;