我正在查询php语言之外的wordpress mysql db,但我不知道如何解决以下问题:每个帖子都有具有一定价值的分类法。我想过滤一个叫做“ calificaciones”的值。这是我当前的查询:
SELECT wp_posts.post_title, IF (wp_term_taxonomy.taxonomy = 'calificaciones', wp_terms.slug, 'no') as calificacion
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)
AND wp_posts.post_type = 'ultimas-noticias'
#AND wp_term_taxonomy.taxonomy = 'calificaciones'
该查询的问题是我将多次获得post_title(因为每个帖子还有其他分类法)。如果帖子具有该分类法值,我想获得该帖子的标题和一个标志,是-否。
最佳答案
可以将子查询用于calificacion
列,而不是在同一查询上加入分类法。
SELECT wp_posts.post_title, CASE WHEN EXISTS((SELECT *
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_term_taxonomy.taxonomy = 'calificaciones'
)) THEN 'YES' ELSE 'NO' END as calificacion
FROM wp_posts
WHERE wp_posts.post_type = 'ultimas-noticias'
关于mysql - 筛选没有特定值的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37843948/