2017-01-19 71 views
0

我有一個查詢,這是一個wordpress查詢的自定義搜索,它工作正常,但這裏是一些會員訂閱和用戶無法找到帖子,如果論壇是隻爲「金」訂閱SQL NOT LIKE「%term%」mot工作

而現在它顯示我想修改查詢到像顯示所有除AND (post_forum_meta.meta_value NOT LIKE '%gold%')

我加入的職位元所有用戶 所有帖子(免費和金)通過ID發佈父母論壇,然後尋找meta_value(免費/金)

SELECT SQL_CALC_FOUND_ROWS 
DISTINCT wp_posts.ID FROM wp_posts 
LEFT JOIN wp_postmeta as meta_1 ON wp_posts.ID = meta_1.post_id 
LEFT JOIN wp_postmeta as event_venue_meta ON meta_1.meta_value = event_venue_meta.post_id 

LEFT JOIN wp_postmeta as post_forum_meta ON meta_1.meta_value = post_forum_meta.post_id AND meta_1.meta_key='_bbp_forum_id' 

LEFT JOIN wp_term_relationships as tax_relation ON wp_posts.ID = tax_relation.object_id 
INNER JOIN wp_term_taxonomy as tax_term ON tax_term.term_taxonomy_id = tax_relation.term_taxonomy_id 
INNER JOIN wp_terms as term ON term.term_id = tax_term.term_id 
LEFT JOIN wp_icl_translations t 


ON wp_posts.ID = t.element_id 
AND t.element_type = CONCAT('post_', wp_posts.post_type) 
WHERE 1=1 
AND (((wp_posts.post_title LIKE '%search%') 
AND (post_forum_meta.meta_value NOT LIKE '%gold%') 
OR (meta_1.meta_key='_tribe_event_speaker' AND meta_1.meta_value LIKE '%search%') 
OR (meta_1.meta_key='_EventStartDate' AND meta_1.meta_value LIKE '%search%') 
OR (event_venue_meta.meta_key='_VenueAddress' AND event_venue_meta.meta_value LIKE '%search%') 
OR (term.name LIKE '%search%') 
OR (wp_posts.post_excerpt LIKE '%search%') 
OR (wp_posts.post_content LIKE '%search%') 

)) 
AND wp_posts.post_type IN ('post', 'tribe_events', 'topic') 
AND ((wp_posts.post_status = 'publish')) 
AND ((t.language_code = 'de' AND wp_posts.post_type IN ('post','page','forum','product','product_variation','tribe_venue','tribe_organizer','tribe_events','tribe_wooticket') )); 

我曾嘗試用NULL NOT LIKE等,但沒有

所以我需要的是讓所有除 「金」 的帖子... 這裏是我的表:

post to hide from search post parent forum with capability

+0

AND和OR沒有括號 - 總是導致問題 –

回答

0

只需將條件移入您的連接:

LEFT JOIN wp_postmeta as post_forum_meta ON meta_1.meta_value = post_forum_meta.post_id AND 
    meta_1.meta_key='_bbp_forum_id' AND 
    post_forum_meta.meta_value NOT LIKE '%gold%' 

這將從數據集中完全排除「黃金」項目nd將簡化底部的條件語句。

或者,您可以使用圓括號在WHERE子句中設置操作的先例。它就像基本的數學一樣 - 記得「PEMDAS」?

WHERE 1=1 
    AND (
    (
     (
     wp_posts.post_title LIKE '%search%' 
     AND post_forum_meta.meta_value NOT LIKE '%gold%' 
    ) 
     AND (
     (meta_1.meta_key='_tribe_event_speaker' AND meta_1.meta_value LIKE '%search%') 
     OR (meta_1.meta_key='_EventStartDate' AND meta_1.meta_value LIKE '%search%') 
     OR (event_venue_meta.meta_key='_VenueAddress' AND event_venue_meta.meta_value LIKE '%search%') 
     OR (term.name LIKE '%search%') 
     OR (wp_posts.post_excerpt LIKE '%search%') 
     OR (wp_posts.post_content LIKE '%search%') 
    ) 
    ) 
    AND wp_posts.post_type IN ('post', 'tribe_events', 'topic') 
    AND wp_posts.post_status = 'publish' 
    AND (
     t.language_code = 'de' 
     AND wp_posts.post_type IN ('post','page','forum','product','product_variation','tribe_venue','tribe_organizer','tribe_events','tribe_wooticket') 
    ) 
); 

你只需要確保你想排除的條件被正確地嵌套在正確的parens集合內。現在,您的「不相似」聲明被這些「或」條件之一所覆蓋。

您可能會發現使用上面提到的第一個選項會更高效 - 連接更小的數據集可能會導致性能更高的查詢。

+0

謝謝,男人,但是當我修改這樣我只得到「金」 任何想法? – anper

+0

@你只得到「黃金」?這很奇怪,因爲該聲明說「不喜歡」。您在該領域尋找的實際價值是多少?它是一個單一的值,還是在該列中以逗號分隔的值列表? –

0

AND(((wp_posts.post_title LIKE '%search%') AND(post_forum_meta.meta_value NOT LIKE '%gold%')

嘗試<>代替

0

UPDATE

跟這個傢伙聊天后,查詢變得

SELECT p.ID, GROUP_CONCAT(m2.meta_key SEPARATOR " ") as is_paid 
FROM wp_posts p 
LEFT JOIN wp_postmeta AS m1 ON p.ID = m1.post_id 
LEFT JOIN wp_postmeta as event_venue_meta ON m1.meta_value = event_venue_meta.post_id 
LEFT JOIN wp_postmeta AS m2 ON m1.meta_value = m2.post_id AND m1.meta_key = '_bbp_forum_id' 
LEFT JOIN wp_postmeta AS m3 ON m2.post_id = m3.post_id AND m3.meta_key = 'groups-groups_read_post' 
LEFT JOIN wp_icl_translations t ON p.ID = t.element_id AND t.element_type = CONCAT('post_', p.post_type) 
WHERE p.post_title LIKE '%search%' 
GROUP BY p.ID 
HAVING is_paid NOT LIKE "%groups-groups_read_post%" OR is_paid IS NULL 
+0

嗨,感謝您的訣竅,它的工作!我改變了一點,現在它返回正確的結果。謝謝!! – anper