搜索句子中任何存储单词的出现 (MySQL) [英] Search for occurence of any stored words within a sentence (MySQL)

查看:50
本文介绍了搜索句子中任何存储单词的出现 (MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到与我要问的问题足够接近的问题,所以这是我的问题:

I have not been able to find a question close enough to what I am asking, so here is my problem:

我有一个存储在 MySQL 表中的列入黑名单的单词列表.然后我有一句话.我需要构建一个 MySQL 查询,在其中搜索句子中任何列入黑名单的单词的出现.

I have a list of blacklisted words stored in a MySQL table. Then I have a sentence. I would need to construct a MySQL query, where I search for occurence of any of the blacklisted words in the sentence.

如果只有一个匹配项,搜索可能会停止,因为这句话是不可接受的.

If there is just one match, the search may stop, as the sentence is not acceptable.

谁能帮我构造这个查询?谢谢!

Can anyone help me construct this query? Thanks!

如果可能,我想避免在 PHP 和 MySQL 之间跳转.我可以有两千个或更多列入黑名单的单词.我想将我的文件作为字符串/变量提交到 MySql,而不是从中构建表.

If possible, I would like to avoid jumping beween PHP and MySQL. I can have two thousands or more blacklisted words. I would like to submit my file as a string/variable into the MySql, not to build a table from it.

我得到的最接近的一行 SQL 是:SELECT 关键字, STRCMP('this is my sentence with blacklisted word', keyword) FROM blacklist;

The closest one line SQL I get is: SELECT keyword, STRCMP('this is my sentence with blacklisted word', keyword) FROM blacklist;

也许,我的线路朝着好的方向发展,如果找到匹配项,可以改进为简单地返回 TRUE 或 FALSE?

Maybe, my line goes in a good direction and can be improved to simply return TRUE or FALSE if a match was found?

推荐答案

经过大量实验,我找到了自己问题的答案:

After a lot of experimentation, I have found an answer to my own question:

SELECT SUM('this is my windows xp file' LIKE CONCAT('%', keyword, '%')) AS result FROM blacklist;

无需在 PHP 中进行多次查询或预处理任何内容,也无需在 MySQL 和 PHP 之间进行多次跳转.

No need for multiple queries or to preprocess anything in PHP or multiple jumps between MySQL and PHP.

这篇关于搜索句子中任何存储单词的出现 (MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆