0

我正在使用三类复选框来过滤来自 WordPress 的帖子(这个问题是关于查询的),并且我知道事实上我应该使用以下查询返回结果(我根据过滤器中选择的内容自动生成第 7 行):

SELECT * FROM wp_posts 
LEFT JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id) 
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_terms.term_id = wp_term_taxonomy.term_id) 
WHERE 
    wp_terms.slug IN ('academia') AND wp_terms.slug IN ('early-career') 
    AND wp_term_taxonomy.taxonomy = 'jobtype' 
    AND wp_posts.post_status = 'publish' 
    AND wp_posts.post_type = 'jobs' 
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

我至少有 5 个帖子属于academiaAND类别early-career

如果我将第 7 行更改为只有一个IN语句,它会很好地过滤一个类别:

wp_terms.slug IN ('academia','journalism') //More than one from same category
AND wp_term_taxonomy.taxonomy = 'jobtype'......

它会找到所有学术或新闻工作(正是我所期望的),但是当我添加另一个IN语句时,没有返回任何内容(即使我有符合逻辑的帖子)。

谁能告诉我是否有问题(可能是IN值和值之间的空格?),因为我已经在这几天没有运气了。

任何帮助是极大的赞赏!谢谢!

PS如果有人想看看这个网站(看看我的意思):http ://www.libertyguide.com/jobs

目前我有一个 globalOR而不是 any IN,直到我让它工作。

4

2 回答 2

0

在子句中指定多个值IN表示该行必须与这些值中的一个匹配。指定 2 个IN子句表明该行必须同时匹配 BOTH,这是不可能的(varchar 不能同时是“学术”和“早期职业”)。

尝试将以下内容添加到查询的末尾:

HAVING COUNT(*) > [NUMBER OF VALUES SELECTED]

所以它变成:

SELECT * FROM wp_posts 
LEFT JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id) 
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_terms.term_id = wp_term_taxonomy.term_id) 
WHERE 
    wp_terms.slug IN ('academia', 'early-career') 
    AND wp_term_taxonomy.taxonomy = 'jobtype' 
    AND wp_posts.post_status = 'publish' 
    AND wp_posts.post_type = 'jobs' 
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
HAVING COUNT(*) = 2
于 2012-04-27T17:56:32.777 回答
0
SELECT * FROM wp_posts 
LEFT JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id) 
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 wt1 ON(wt1.term_id = wp_term_taxonomy.term_id) 
LEFT JOIN wp_terms wt2 ON(wt2.term_id = wp_term_taxonomy.term_id) 
WHERE 
   wt1.slug = 'academia' AND wt2.slug = 'early-career' 
    AND wp_term_taxonomy.taxonomy = 'jobtype' 
    AND wp_posts.post_status = 'publish' 
    AND wp_posts.post_type = 'jobs' 
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

编辑 要支持 N 个类别,您可以试试这个:

SELECT * FROM wp_posts 
LEFT JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id) 
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) 
WHERE 
    wp_term_taxonomy.taxonomy = 'jobtype' 
    AND wp_posts.post_status = 'publish' 
    AND wp_posts.post_type = 'jobs' 
    AND wp_term_taxonomy.term_id IN (SELECT term_id from wp_terms where slug = 'academia')
    AND wp_term_taxonomy.term_id IN (SELECT term_id from wp_terms where slug = 'early-career')
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

您可以根据需要在代码中多次添加 AND wp_term_taxonomy... 行。

于 2012-04-27T17:59:56.703 回答