如何在SQL语句中排除某些类别

时间:2011-02-23 16:08:25

标签: php sql wordpress

这是我目前从wordpress wp_post表中获取热门帖子的代码。如何排除3或4类别中的那些?

$popularposts = "SELECT ID,post_title FROM {$wpdb->prefix}posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY comment_count DESC LIMIT 0,".$pop_posts;
$posts = $wpdb->get_results($popularposts);

2 个答案:

答案 0 :(得分:1)

在'发布'之后添加(假设类别的字段是类别)

and categorie not in ('3', '4')

或者,如果类别是数字:

and (categorie < 3 or categeorie > 4)

答案 1 :(得分:1)

通过网络和stackoverflow挖掘,几乎解决了问题。我还需要添加

ORDER BY comment_count DESC LIMIT 0,".$pop_posts

以下代码中的某处。

$popularposts  = "SELECT * FROM $wpdb->posts 
INNER JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)  
INNER JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)  

WHERE ($wpdb->term_taxonomy.term_id <> 3  
   AND $wpdb->term_taxonomy.term_id <> 4  
   AND $wpdb->term_taxonomy.taxonomy = 'category'   
   AND $wpdb->posts.post_type = 'post'  
   AND $wpdb->posts.post_status = 'publish')";