0

我编写此查询以查找具有自定义字段“语言”且其值为“英语”的帖子或帖子根本没有自定义字段“语言”。

SELECT DISTINCT wp_posts.* FROM wp_posts, wp_postmeta 
WHERE wp_posts.ID = wp_postmeta.post_id  
AND (wp_postmeta.meta_key = 'language' AND wp_postmeta.meta_value like '%English%' OR NOT EXISTS (SELECT * FROM wp_posts, wp_postmeta Where wp_postmeta.meta_key = 'language' ))

但是,它只返回具有自定义字段“语言”且其值为“英语”的帖子

请帮我

4

2 回答 2

1

在 where 条件下,你应该小心括号 -

尝试以下查询 -

SELECT DISTINCT wp_posts.* FROM wp_posts , wp_postmeta 
WHERE wp_posts.ID = wp_postmeta.post_id  
AND ((wp_postmeta.meta_key = 'language' AND wp_postmeta.meta_value like '%English%' ) OR NOT EXISTS (SELECT * FROM wp_posts, wp_postmeta Where wp_postmeta.meta_key = 'language' ))

还有一件事,您需要对存在子查询中的 ID 的一个条件,如下所示 -

SELECT DISTINCT wp_posts.* FROM wp_posts , wp_postmeta 
    WHERE wp_posts.ID = wp_postmeta.post_id  
    AND ((wp_postmeta.meta_key = 'language' AND wp_postmeta.meta_value like '%English%' ) OR NOT EXISTS (SELECT 1 FROM wp_postmeta wp_postmeta1 Where wp_postmeta1.meta_key = 'language' and wp_postmeta1.post_id = wp_postmeta.post_id ))
于 2013-04-07T05:56:32.240 回答
0

试试这个,

SELECT DISTINCT wp_posts.* FROM wp_posts, wp_postmeta 
WHERE wp_posts.ID = wp_postmeta.post_id  
AND (wp_postmeta.meta_key = 'language' AND 
     wp_postmeta.meta_value like '%English%')
OR (wp_postmeta.meta_key != 'language');  --Not sure if you want (!= language) or null. Change as required.
于 2013-04-07T06:16:27.077 回答