我的 google-fu 让我失望了,如果有人能为我指出概念或术语的正确方向,那就太好了。
我正在尝试从 mysql 数据库中获取行。假设我有一个等于'DEF' 的变量。 我想在数据库中以任意顺序搜索仅包含 DEF 的记录。
示例列,
ABC
BC
D
DEX
DEF
DEFF
ED
EDF
FED
FEED
会回来 D、DEF、ED、EDF、FED
最佳答案
您需要的是一个用户定义的函数来检查字符串是否匹配。这是一个:
delimiter //
create function permutes(needles varchar(255), haystack varchar(255)) returns bool
begin
declare needles_position, first_occurance int;
set needles_position = length(needles);
while needles_position > 0 do
set first_occurance = instr(haystack, substring(needles, needles_position, 1));
if first_occurance = 0 then
return false;
end if;
set haystack = (select concat(substring(haystack,1,first_occurance-1), substring(haystack,first_occurance+1)));
set needles_position = needles_position - 1;
end while;
return true;
end//
现在你会得到你想要的:
select example_column from your_table where permutes(example_column ,'def');
该函数的作用是获取所有针字符并查看它们是否存在于 haystack 中。每根针都会在检查下一根针之前从干草堆中取出,因此您不会得到 double 。
关于php - MySQL 查询或正则表达式仅查找模式 D、E、F,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23218557/