我会尝试使用正则表达式拆分单词并将它们放入一个临时表中,您可以使用它加入您的 Person 表。
正则表达式可能看起来像这样
preg_match_all('/(\S+)*\s+/im', $input, $value, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($value[0]); $i++) {
# Matched text = $result[0][$i];
}
在 SQL Server 中查找结果的脚本可能如下所示。我没有足够的 MySQL 知识来提出 MySQL 的替代方案,但您应该明白这一点。
这个想法的要点是在您希望搜索的每一列上加入输入表。该where
子句处理输入值至少在有输入值的列数中找到的事实。
DECLARE @Table TABLE (Name VARCHAR(20), Surname VARCHAR(20), Address VARCHAR(20), IDCard VARCHAR(20), Town VARCHAR(20), Street VARCHAR(20))
DECLARE @Inputs TABLE (Value VARCHAR(32))
INSERT INTO @Table VALUES ('Doe', 'John', 'Dontknow', 'Dontknow', 'US', 'Skate')
INSERT INTO @Inputs VALUES ('%John%')
INSERT INTO @Inputs VALUES ('%Doe%')
INSERT INTO @Inputs VALUES ('%Skate%')
SELECT t.*
FROM @Table t
LEFT OUTER JOIN @Inputs i_name ON t.Name LIKE i_name.Value
LEFT OUTER JOIN @Inputs i_surname ON t.SurName LIKE i_surname.Value
LEFT OUTER JOIN @Inputs i_address ON t.Address LIKE i_address.Value
LEFT OUTER JOIN @Inputs i_idcard ON t.IDCard LIKE i_idcard.Value
LEFT OUTER JOIN @Inputs i_town ON t.Town LIKE i_town.Value
LEFT OUTER JOIN @Inputs i_street ON t.Street LIKE i_street.Value
CROSS APPLY (SELECT inputCount = COUNT(*) FROM @Inputs) cnt
WHERE cnt.inputCount <=
CASE WHEN i_name.Value IS NULL THEN 0 ELSE 1 END
+ CASE WHEN i_surname.Value IS NULL THEN 0 ELSE 1 END
+ CASE WHEN i_address.Value IS NULL THEN 0 ELSE 1 END
+ CASE WHEN i_idcard.Value IS NULL THEN 0 ELSE 1 END
+ CASE WHEN i_town.Value IS NULL THEN 0 ELSE 1 END
+ CASE WHEN i_street.Value IS NULL THEN 0 ELSE 1 END