MySQL meta_query,如何使用关系 OR & AND 进行搜索?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18401879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:38:07  来源:igfitidea点击:

meta_query, how to search using both relation OR & AND?

mysqlsqlwordpressmeta-query

提问by Angelica Santibanez

Resolved: See answer below.

已解决:请参阅下面的答案。



I have a custom post type called BOOKS. It has several custom fields, named: TITLE, AUTHOR, GENRE, RATING. How do I fix my meta_querycode below so that only booksthat have the search word in the custom fields: title, author, genreWITH EXACTLYthe ratingspecified in my search form, gets displayed in the results?

我有一个名为BOOKS的自定义帖子类型。它有几个自定义字段,名称为:TITLE, AUTHOR, GENRE, RATING。如何解决我meta_query下面的代码,以便只有有在自定义字段搜索词:titleauthorgenrerating我的搜索形式规定,被显示在结果?

I have made a custom search form; a text area that will search through the title, authorand genre; and a dropdown that will search for the rating. The meta_queryI made below only searches through the title, author, and genre. But I am now stumped in how to add the code for the rating.

我做了一个自定义的搜索表单;将搜索title,author和的文本区域genre;和一个下拉菜单,将搜索rating. 在meta_query我做了下面仅通过搜索的titleauthorgenre。但我现在对如何为rating.

This is how I visually imagined it with meta_query relation: (title OR author OR genre) AND rating

这就是我通过 meta_query 关系直观地想象它的方式:(标题或作者或流派)和评级

$args = array(
        'relation' => 'OR',
          array(
             'key' => 'title',
             'value' => $searchvalue,
             'compare' => 'LIKE'
          );
          array(
             'key' => 'author',
             'value' => $searchvalue,
             'compare' => 'LIKE'
          );
          array(
             'key' => 'genre',
             'value' => $searchvalue,
             'compare' => 'LIKE'
          );
), 
        array(
        'relation' => 'AND', 
          array(
             'key' => 'rating',
             'value' => $ratingvalue,
             'compare' => '=',
             'type' => 'NUMERIC'
          ));

I would extremely appreciate your help and advice.

我非常感谢您的帮助和建议。

回答by Angelica Santibanez

I found the solution with some help. The code below worked perfectly.

我在一些帮助下找到了解决方案。下面的代码工作得很好。

    $args => array(
        'relation' => 'AND',
        array(
            'relation' => 'OR',
            array(
                'key' => 'title',
                'value' => $searchvalue,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'author',
                'value' => $searchvalue,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'genre',
                'value' => $searchvalue,
                'compare' => 'LIKE'
            )
        ),
        array(
            'key' => 'rating',
            'value' => $ratingvalue,
            'compare' => '=',
            'type' => 'NUMERIC'

        )
    )
);

回答by dipak_pusti

After a bit trial and error I find a solution to this. This is logical I mean meta_query does not supports the array for field "key", but giving the array in "key", I'm getting the perfect solution. I may be sound crazy but it's working like charm.

经过一番试验和错误后,我找到了解决方案。这是合乎逻辑的,我的意思是 meta_query 不支持字段“key”的数组,但是在“key”中给出数组,我得到了完美的解决方案。我可能听起来很疯狂,但它就像魅力一样工作。

$args => array(
    'relation' => 'AND',
    array(
        'key' => array('title','author','genre',),
        'value' => $searchvalue,
        'compare' => '='
    ),
    array(
        'key' => 'rating',
        'value' => $ratingvalue,
        'compare' => '=',
        'type' => 'NUMERIC'

    )
)

You only get a warning for "trim()" as we are passing an array instead of a string. Suppress that warning or Please add something if you find a better solution.

当我们传递的是数组而不是字符串时,您只会收到“trim()”的警告。禁止该警告,或者如果您找到更好的解决方案,请添加一些内容。