我有以下查询
SELECT *
FROM `articles`
WHERE (
(
UCASE( `title` ) LIKE UCASE( '% Fishoil %' )
AND UCASE( `title` ) LIKE UCASE( '% for %' )
AND UCASE( `title` ) LIKE UCASE( '% more %' )
AND UCASE( `title` ) LIKE UCASE( '% musclemass %' )
)
OR (
UCASE( `text` ) LIKE UCASE( '% Fishoil %' )
AND UCASE( `text` ) LIKE UCASE( '% for %' )
AND UCASE( `text` ) LIKE UCASE( '% more %' )
AND UCASE( `text` ) LIKE UCASE( '% musclemass %' )
)
OR (
UCASE( `source` ) LIKE UCASE( '% Fishoil %' )
AND UCASE( `source` ) LIKE UCASE( '% for %' )
AND UCASE( `source` ) LIKE UCASE( '% more %' )
AND UCASE( `source` ) LIKE UCASE( '% musclemass %' )
)
OR (
UCASE( `unique` ) LIKE UCASE( '% Fishoil %' )
AND UCASE( `unique` ) LIKE UCASE( '% for %' )
AND UCASE( `unique` ) LIKE UCASE( '% more %' )
AND UCASE( `unique` ) LIKE UCASE( '% musclemass %' )
)
)
ORDER BY `year` DESC
我想做的是改变
UCASE( `tile` ) LIKE UCASE( '% Fishoil %' )
进入
UCASE( `title` ) LIKE UCASE( '%Fishoil %' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil,%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil.%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil:%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil;%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil\'%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil"%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil!%' )
OR
UCASE( `title` ) LIKE UCASE( '%Fishoil?%' )
但在我看来,这似乎使查询变得异常复杂,因为它必须进行多次匹配。是否有一种正则表达式匹配可以一次性匹配“搜索词+特殊字符”?
如果是这样,与它匹配的最佳方法是什么?
这是生成关键字搜索字符串的 php 代码
$specialchars = array(' ',',','.',':',';',mysql_real_escape_string("'"),'"','!','?');
foreach($seek as $searchword)
{
foreach($specialchars as $char)
{
$seeker[] = "LIKE UCASE( '%$searchword".$char."%' )";
}
$temp = implode(" OR ",$seeker);
echo $temp;
}