2015-02-09 79 views
0
的帖子數

我已經使用高級自定義字段插件創建了自定義字段。這個字段被稱爲狀態,默認情況下等於即將到來,然後它將在以後更改爲正確或錯誤。返回元值等於

我然後試圖查詢狀態元值等於正確或錯誤的所有帖子。不過,即使我已經在下面的查詢中設置了它,它仍然會返回所有帖子的計數。我已經仔細檢查並計算了有多少帖子等於正確或錯誤,但仍然得到錯誤的數量。我究竟做錯了什麼?

SELECT count(DISTINCT posts.ID) as count, AVG(odds.meta_value) as ave 
    FROM $wpdb->posts posts 
    LEFT JOIN $wpdb->postmeta stat 
      ON posts.ID = stat.post_id 
      AND stat.meta_key = 'status' 
    LEFT JOIN $wpdb->postmeta odds 
      ON posts.ID = odds.post_id 
      AND odds.meta_key = 'odds' 
    LEFT JOIN $wpdb->term_relationships tr ON posts.ID = tr.object_id 
    LEFT JOIN $wpdb->term_taxonomy t ON tr.term_taxonomy_id = t.term_taxonomy_id 
    WHERE stat.meta_value = 'wrong' 
    OR stat.meta_value = 'correct' 
    AND posts.post_status  = 'publish' 
    AND t.taxonomy = 'category' 
    AND t.term_id = 4 
    OR t.term_id = 5 
    OR t.term_id = 6 
+0

更改您的WHERE AND,還您可能想要在那些一些括號或OR條件.. – Mihai 2015-02-09 19:20:53

+0

然後返回115這是實際職位以上的方式 – 2015-02-09 19:22:28

回答

0

你應該用括號組合在一起應該是什麼在一起:

SELECT count(DISTINCT posts.ID) as count, AVG(odds.meta_value) as ave 
FROM $wpdb->posts posts 
LEFT JOIN $wpdb->postmeta stat 
    ON posts.ID = stat.post_id 
    AND stat.meta_key = 'status' 
LEFT JOIN $wpdb->postmeta odds 
    ON posts.ID = odds.post_id 
    AND odds.meta_key = 'odds' 
LEFT JOIN $wpdb->term_relationships tr 
    ON posts.ID = tr.object_id 
LEFT JOIN $wpdb->term_taxonomy t 
    ON tr.term_taxonomy_id = t.term_taxonomy_id 
WHERE (stat.meta_value = 'wrong' 
    OR stat.meta_value = 'correct') 
    AND posts.post_status  = 'publish' 
    AND t.taxonomy = 'category' 
    AND (t.term_id = 4 
    OR t.term_id = 5 
    OR t.term_id = 6)