MySQL选择查询字符串中包含的字段值
假设: 短语1:我是蜘蛛侠,我有一个土豆 短语2:我是一只蜘蛛,我喜欢土豆 表:MySQL选择查询字符串中包含的字段值,mysql,sql,tsql,Mysql,Sql,Tsql,假设: 短语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 因此,它应该在表列
column_id | column_str
1 | spiderman
2 | potatoes
我想执行一个SQL查询,其中输入的是短语,结果应该是:
对于短语1:
column_id | column_str
1 | spiderman
对于短语2:
column_id | column_str
2 | potatoes
因此,它应该在表列_str列的输入中找到匹配的子字符串,并返回其关联的id
我还没有错误或尝试的例子,因为我不知道从哪里开始
我所知道的是有像%匹配器这样的
%匹配器。但这恰恰相反,类似于_like%
中的%
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 |
+--------+
您应该考虑一些棘手的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 |
+--------+
您只需要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;
请参阅以供参考。您所需要的只是安装功能:
--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、MS SQL Server还是Sybase?您使用的是MySQL、MS SQL Server还是Sybase?