[英]In MySQL how to write SQL to search for words in a field?
create table tbl (
id int,
comment varchar(255),
primary key (id)
);
insert into tbl (id, comment) values ('1', 'dumb,');
insert into tbl (id, comment) values ('2', 'duuumb,');
insert into tbl (id, comment) values ('3', 'dummb');
insert into tbl (id, comment) values ('4', 'duummb');
insert into tbl (id, comment) values ('5', 'very dumb person');
select comment, soundex(comment)
from tbl;
Result: 结果:
+------------------+------------------+
| comment | soundex(comment) |
+------------------+------------------+
| dumb, | D510 |
| duuumb, | D510 |
| dummb | D510 |
| duummb | D510 |
| very dumb person | V6351625 |
+------------------+------------------+
I want to find all rows containing 'dumb', including all typos and variations, anywhere in the field. 我想在字段中的任何地方找到所有包含“哑”的行,包括所有错字和变化。
select comment
from tbl
where soundex(comment) like '%D510%'
This fails to get the final row #5, how can I also get that row? 这无法获得最后的第5行,我又如何获得该行? If there is a better solution than soundex() that would be fine. 如果有比soundex()更好的解决方案,那就没问题了。
This will work for your particular example: 这将适用于您的特定示例:
select comment
from tbl
where soundex(comment) like '%D510%' or comment like '%dumb%';
It won't find misspellings in the comment. 它不会在评论中发现拼写错误。
EDIT: 编辑:
You could do something like this: 您可以执行以下操作:
select comment
from tbl
where soundex(comment) = soundex('dumb') or
soundex(substring_index(substring_index(comment, ' ', 2), -1) = soundex('dumb') or
soundex(substring_index(substring_index(comment, ' ', 3), -1) = soundex('dumb') or
soundex(substring_index(substring_index(comment, ' ', 4), -1) = soundex('dumb') or
soundex(substring_index(substring_index(comment, ' ', 5), -1) = soundex('dumb');
A bit brute force. 有点蛮力。
The need to do this suggests that you should consider a full text index. 需要这样做建议您应该考虑全文索引。
Assuming you want the whole field and not just the matching element then this should work as soundex will never find an element in the middle of an other element; 假设您需要整个字段,而不仅仅是匹配的元素,那么这应该起作用,因为soundex永远不会在另一个元素的中间找到一个元素。
select comment from tbl where (soundex(comment) like '%D510%' or comment like '%d%mb') 选择来自tbl的评论,其中(soundex(comment)如'%D510%'或'%d%mb'之类的评论)
Edit Changed like for cases where the U is something else ie Damb which gives a soundex of D510 编辑已更改,例如对于U是其他东西的情况,即Damb,其声音为D510
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.