I have the following query
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
What I wish to do, is change
UCASE( `tile` ) LIKE UCASE( '% Fishoil %' )
into
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?%' )
But this seems to me to make the query unnessarily complex since it'll have to do the match several times. Is there a sort of regex match to match against the "searchterm + special character" in one go?
If so, what would be the best way to match against it?
This is the php code to generate the keyword search string
$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;
}
I suggest that you use regular expressions, since they are much more powerful than the wildcard LIKE
syntax. By using regex, you can fit the whole set of conditions into one REGEXP
call (per column).