从分类中只选择一个term_id

时间:2017-12-08 03:18:24

标签: mysql sql wordpress

我有一个自定义的帖子类型和分类,我可以将更多的值放在分类法中,在这种情况下,分类法就是城市。我想查询结果是只有一个值集的帖子。示例:我想要的所有帖子都只是在这种情况下,#term; term_id = 3"那是" sao-paulo-sp"。

但我有一些帖子有多个值设置,例如" sao-paulo-sp,city 2,city 3 city 4等等#34;这种帖子一直出现在我的结果中。

<select
  class="i-select"
  v-model="value"
  @change="onChange($event)">
  <option
    v-for="option in options"
    :value="option.value"
    @click.stop.prevent="option.method($event)">
    {{ option.text }}
  </option>
</select>

我希望结果是那些只有&#34; sao-paulo-sp&#34;或&#34; term_id = 3&#39;作为他对分类学城市的价值

2 个答案:

答案 0 :(得分:0)

如果我理解正确,你需要这样的东西:

SELECT wp.ID, wp.post_title, wt.term_id
FROM wp_posts wp INNER JOIN
     wp_term_relationships wtr
     ON wp.`ID` = wtr.`object_id` INNER JOIN
     wp_term_taxonomy wtt
     ON wtr.`term_taxonomy_id` = wtt.`term_taxonomy_id` INNER JOIN 
     wp_terms wt
     ON wt.`term_id` = wtt.`term_id`
WHERE wtt.taxonomy = 'cities'
GROUP BY wp.ID
HAVING COUNT(*) = 1 AND MAX(wt.slug) = 'sao-paulo-sp'
ORDER BY wp.ID;

我不确定wt.term_id列表中SELECT正在做什么。

答案 1 :(得分:0)

Srry,我的英语不足以指明我的问题,如果有人帮助我解决了我的问题,在一个与term_id不同于tem_id 3的选择中进行NOT IN。

SELECT wp.ID, wp.post_title, wt.slug
   FROM wp_posts wp
   INNER JOIN wp_term_relationships wtr ON wp.`ID` = wtr.`object_id`
   INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = 
   wtt.`term_taxonomy_id`
INNER JOIN wp_terms wt ON wt.`term_id` = wtt.`term_id`
WHERE wtt.taxonomy = 'cidades'
AND wp.ID NOT IN
    (SELECT wp.ID
    FROM wp_posts wp
    INNER JOIN wp_term_relationships wtr ON wp.`ID` = wtr.`object_id`
    INNER JOIN wp_term_taxonomy wtt ON wtr.`term_taxonomy_id` = 
    wtt.`term_taxonomy_id`
    WHERE wtt.taxonomy ='cidades' and wtt.term_id <> 3)
ORDER BY wp.ID