sql不喜欢“%term%”mot工作

时间:2017-01-19 19:50:40

标签: mysql sql wordpress join left-join

我有一个查询,它是自定义搜索的wordpress查询,它工作正常,但这里有一些会员订阅,如果论坛仅用于“黄金”订阅,用户无法找到帖子

现在它显示所有用户的所有帖子(免费和金牌) 我想将查询修改为显示除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 LIKE NOT等但没有

所以除了“黄金”帖子之外我还需要它... 这是我的表格:

post to hide from search post parent forum with capability

3 个答案:

答案 0 :(得分: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%'

这将完全排除数据集中的“黄金”项目,并简化底部的条件语句。

或者,您可以使用括号在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集中。现在,你的“不喜欢”声明被其中一个“OR”条件所覆盖。

您可能会发现使用我上面提到的第一个选项更加高效 - 加入较小的数据集可以带来更高效的查询。

答案 1 :(得分:0)

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

尝试<>

答案 2 :(得分:0)

<强>更新

与这个人聊天后,查询变为

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