我正在使用MySQL工作台,目前我正在寻找一个函数或其他东西来帮助我检查一个字符串是否在另一个字符串中。例如:
text1 = 'hello , name , world';
如果我有一个类似如下的字符串或varchar
'My name is'
它应该说它包含,因为它上面有'name‘。基本上,我要做的是创建一个评论表,并在插入评论之前创建一个触发器,用于检查comment_text
是否包含任何脏话。这些脏话是这样保存的
从reserved_words中将group_concat(word)选择为文本;
因此,它包含了所有的单词: swear1、swear2、swear3等
如果评论真的包含脏话,我想让它更新某个表。我尝试过使用LIKE, SUBSTR(),CONTAINS(),REGEXP
,但没有成功。
有人能帮帮忙吗?
发布于 2018-06-23 10:14:31
LIKE
就是你想要的。您需要使用%
对其进行包装,以匹配字符串中的任何位置。
WHERE string LIKE '%name%'
如果string
列的值包含字符串name
,则为true。
如果你在表格的某一列中有脏话,你可以把它们连接起来:
SELECT DISTINCT comment
FROM comments AS c
JOIN swear_words AS s ON c.comment LIKE CONCAT('%', s.word, '%')
不要将脏话放在逗号分隔的列表中,这会使在MySQL中单独处理它们变得非常困难。
发布于 2018-06-23 09:00:16
我建议:
where comment regexp replace(swearwords, ',', '|')
您可能希望也可能不希望在正则表达式模式中包含单词分隔符。
发布于 2018-06-23 10:37:30
从reserved_words
表构建您的正则表达式模式:
select CONCAT('(',group_concat(word SEPARATOR '|'),')') from reserved_words
然后对comment
字段运行REGEXP
检查。
运行的演示
create table reserved_words(word varchar(10));
insert into reserved_words values('hello'),('name'),('world');
select 'My name is' REGEXP (select CONCAT('(',group_concat(word SEPARATOR '|'),')') from reserved_words) swear
UNION ALL
select 'My title is' REGEXP (select CONCAT('(',group_concat(word SEPARATOR '|'),')') from reserved_words) swear
UNION ALL
select 'hello world' REGEXP (select CONCAT('(',group_concat(word SEPARATOR '|'),')') from reserved_words) swear;
结果
swear
1
0
1
https://stackoverflow.com/questions/50997016
复制相似问题