查询字符串中包含的mysql select字段值

mrwjdhj3  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(337)

假设:
短语1:我是蜘蛛侠,我有一个土豆
短语2:我是一只蜘蛛,我喜欢土豆
表格:

column_id | column_str
1         | spiderman
2         | potatoes

我想执行一个sql查询,其中输入是短语,结果应该是:
对于第1阶段:

column_id | column_str
1         | spiderman

对于第2阶段:

column_id | column_str
2         | potatoes

因此,它应该在表列\u str列的输入中找到匹配的子字符串,并返回其关联的id。
我还没有错误或尝试的例子,因为我不知道从哪里开始。
我只知道 %LIKE% 匹配器。但这恰恰相反,就像 %IN_LIKE% .

nhaq1z21

nhaq1z211#

您应该考虑一些棘手的sql,如下所示:

select dsd
from (select 'potato' as dsd) ss
where (select 'I am spiderman and I have a potato') rlike dsd;

例子:

mysql> select dsd from (select 'potato' as dsd) ss where (select 'I am spiderman and I have a potato') rlike dsd;
+--------+
| dsd    |
+--------+
| potato |
+--------+
rqdpfwrv

rqdpfwrv2#

你只需要 INSTR 功能:

--sample data creation
create table tbl(id int, str varchar(50));
insert into tbl values (1, 'spiderman');
insert into tbl values (2, 'potatoes');

select * from tbl
where instr('I am spiderman and I have a potato', str) > 0;

select * from tbl
where instr('I am a spider and I like potatoes', str) > 0;

见mysql INSTR() 函数以供参考。

相关问题