SQL Query不返回结果

时间:2012-04-27 17:53:45

标签: sql filter

我使用三类复选框来过滤来自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个帖子属于academiaearly-career类别。

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

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

它会找到学术界或新闻界的所有工作(正是我所期望的),但当我添加其他IN语句时,不会返回任何内容(即使我有与逻辑匹配的帖子)

任何人都可以告诉我,如果出现问题(可能是IN和值之间的空格吗?),因为我已经好几天都没有运气了。

非常感谢任何帮助!谢谢!

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

目前我有一个全局OR而不是任何IN,直到我开始工作。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 ...行。