0

我有以下 MySQL 查询(查询 wordpress db):

SELECT wp_posts.ID, wp_posts.post_date, wp_posts.post_content, wp_posts.post_title, wp_terms.name, wp_term_taxonomy.taxonomy
FROM wp_posts
LEFT JOIN wp_term_relationships ON ( wp_term_relationships.object_id = wp_posts.ID ) 
LEFT JOIN wp_term_taxonomy ON ( wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
LEFT JOIN wp_terms ON ( wp_terms.term_id = wp_term_taxonomy.term_id) 
WHERE 1=1
AND wp_terms.name != 'MyTagName'
AND wp_posts.post_type = 'post'
AND (wp_posts.post_status = 'publish')
ORDER BY wp_posts.post_date DESC 
LIMIT 100

我基本上是在尝试停止所有返回带有标签(db table wp_terms)'MyTagName'的'wp_posts'。但是以上似乎所做的就是从返回的行中删除该标签名称,将 wp_post 条目与它所标记的其他标签条目留在其中。

谁能帮我这个?

4

1 回答 1

1
SELECT wp_posts.ID, wp_posts.post_date, wp_posts.post_content, wp_posts.post_title, wp_terms.name, wp_term_taxonomy.taxonomy
FROM wp_posts
JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.ID
JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE NOT EXISTS (
    SELECT 1
    FROM wp_term_relationships
    JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
    JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
    WHERE wp_term_relationships.object_id = wp_posts.ID
        AND wp_terms.name = 'MyTagName'
    )
    AND wp_posts.post_type = 'post'
    AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC 
LIMIT 100

问题是您当前的解决方案没有检查与帖子相关的所有 wp_terms,而只是检查特定的。如果有 5 个术语,其中一个是“MyTagName”,则该单个术语将被排除,但其他 4 个仍将被加入。

现在关于:

AND wp_posts.post_type = 'post'
AND wp_posts.post_status = 'publish'

您不需要将它们放在大的 NOT EXISTS 检查中,因为如果其中任何一个为假,则无论如何都不会收到该帖子。

于 2012-05-24T11:39:23.180 回答