2016-06-15 78 views
0

我在查詢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多次(因爲有其他分類法對每個崗位)。如果帖子具有該分類標準值,我希望獲得帖子標題和標誌是否。

回答

1

而不是在同一查詢上加入分類法,您可以使用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' 
+0

工作就像一個魅力 – Rod0n

0

可以使用select不同

SELECT distinct 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'