Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有多个单词的MySQL类通配符查询_Mysql - Fatal编程技术网

具有多个单词的MySQL类通配符查询

具有多个单词的MySQL类通配符查询,mysql,Mysql,我有一个mysql查询,如下所示 $query="SELECT name,activity FROM appid where result > 5 AND name LIKE :term ORDER BY name ASC LIMIT 0,40"; $result = $pdo->prepare($query); $result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR); $result->execu

我有一个mysql查询,如下所示

$query="SELECT name,activity FROM appid 
where result > 5 AND name LIKE :term ORDER BY name ASC LIMIT 0,40";

$result = $pdo->prepare($query);

$result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR);
$result->execute();
我想做的就是这样

我想找一个像这样的条目

“新闻与天气”

但是当我打字的时候

“新闻天气”


它当然不会找到它。我如何才能键入并检索该条目?

我认为唯一可行的方法是通过代码:

选项A:在代码中将查询参数中的空格替换为“%”,但这当然会使多个单词有序

选项B:在空格上拆分参数,并根据需要使用尽可能多的like动态构造查询,为每个like添加额外的“:termN”参数。

可以做到这一点:

select *
from appid
where name rlike 'news|weather' -- Matches 'news' or 'weather'
另一个例子:

select *
from appid
where name rlike 'news.*weather' -- Matches 'news' and 'wether' 
                                 -- with any characters in-between or none at all
                                 -- (ordered)
再多说一句:

select *
from appid
where name rlike '^news.{1,}weather$' -- Matches any text that starts with 'news'
                                      -- and has at least one character before
                                      -- ending with 'weather'
常规ESPression可用于在文本字段上创建非常复杂的过滤器。阅读上面的链接了解更多信息


如果您可以向表中添加全文索引,这可能是更好的方法。具体而言,a:


在空格上创建一个单词拆分数组,并为数组中的每个值创建一个
或类似的
。更简单:使用正则表达式(见下面的答案)我建议您阅读关于更好的选项的文章哦,是的,我忘了它有这些;在我经常使用的数据中,我并没有真正使用它们。当有人把它们介绍给我时,我也说了同样的话。。。现在我不能离开他们;)大多数情况下,我在索引字段中搜索“以ABC开头”的字符串,因此正则表达式将是一个不利因素,除非他们自上次检查以来对其进行了优化,以便在表达式以固定文本开头时可以使用索引。这看起来不错,但如何将其实现到使用用户搜索中的$\u GET的查询中?
select *
from appid
where match(name) against (+news +weather)