0

我正在运行一个 Wordpress 站点,并且正在制作一个小部件,它可以为我提供过去 8 天内阅读次数最多的所有帖子。

当我运行这个 SQL 字符串时,我得到了所有带有类别的帖子,但是当帖子不在一个类别中时,我不明白。

如何获取属于某个类别的所有帖子,以及所有不在某个类别中的帖子?

我的 sql 字符串

$sql ="SELECT wp_posts.post_date, wp_posts.post_title, wp_posts.post_name, wp_posts.post_content, wp_terms.name, wp_terms.slug, wp_postmeta.meta_value FROM wp_posts
        INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
        INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
        INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
        INNER JOIN wp_postmeta ON (wp_postmeta.post_id = wp_posts.ID)
        WHERE (wp_term_taxonomy.taxonomy = 'category'
           AND wp_postmeta.meta_key = 'readCount'
           AND wp_posts.post_type = 'post'
           AND DATE(wp_posts.post_date) >= '$eight_days_ago'
           AND wp_posts.post_status = 'publish')
           GROUP BY wp_posts.ID
           ORDER BY wp_postmeta.meta_value DESC
           LIMIT 5";
4

2 回答 2

1

I think this will work. You need to change the joins to left outer joins and then check for NULL in the where clause:

SELECT wp_posts.post_date, wp_posts.post_title, wp_posts.post_name, wp_posts.post_content, wp_terms.name, wp_terms.slug, wp_postmeta.meta_value FROM wp_posts
        LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
        LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
        LEFT JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
        LEFT JOIN wp_postmeta ON (wp_postmeta.post_id = wp_posts.ID)
        WHERE ((wp_term_taxonomy.taxonomy = 'category') or (wp_term_taxonomy.taxonomy is NULL))
           AND wp_postmeta.meta_key = 'readCount'
           AND wp_posts.post_type = 'post'
           AND DATE(wp_posts.post_date) >= '$eight_days_ago'
           AND wp_posts.post_status = 'publish')
           GROUP BY wp_posts.ID
           ORDER BY wp_postmeta.meta_value DESC
           LIMIT 5
于 2013-04-17T14:01:59.423 回答
0

让数据库,无论它是什么,检查类别列中的空值:

SELECT wp_posts.post_date, wp_posts.post_title, wp_posts.post_name, wp_posts.post_content, wp_terms.name, wp_terms.slug, wp_postmeta.meta_value FROM wp_posts
        INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
        INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
        INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
        INNER JOIN wp_postmeta ON (wp_postmeta.post_id = wp_posts.ID)
        WHERE ((wp_term_taxonomy.taxonomy = 'category' OR wp_term_taxonomy.taxonomy IS NULL)
           AND wp_postmeta.meta_key = 'readCount'
           AND wp_posts.post_type = 'post'
           AND DATE(wp_posts.post_date) >= '$eight_days_ago'
           AND wp_posts.post_status = 'publish')
           GROUP BY wp_posts.ID
           ORDER BY wp_postmeta.meta_value DESC
           LIMIT 5
于 2013-04-17T13:44:31.810 回答