Is there a way within MySQL to return results using a LIKE
statement that contain or do not contain punctuation characters such as apostrophes and hyphens?
For example if I have one of the following queries:
SELECT * FROM some_table WHERE some_value LIKE "%this-thing%";
or
SELECT * FROM some_table WHERE some_value LIKE "%person's thing%"
Is there a way in which I could obtain the same results as the queries below but with a single LIKE clause?
SELECT * FROM some_table WHERE (some_value LIKE "%this-thing%") OR (some_value LIKE "%this thing%");
or
SELECT * FROM some_table WHERE (some_value LIKE "%person's thing%") OR (some_value LIKE "%persons thing%");
Basically a function that when searching would return a result if either version of the text exists ("person's thing" or "persons thing"), something like:
EDIT
Is there a function within mysql which would return only alpha numeric characters within a column? For example if the column value where "something's & other-stuff" then it would be returned as "somethingsotherstuff"? So a query would look something like:
SELECT * FROM table WHERE magic_function(column_name) LIKE '%words%'
EDIT x2
For the time being I have resolved this by chaining together REPLACE() and replacing all special characters in the column value with "" before doing the comparision. Value being compared has also been stripped of all special characters.
Best Answer
I had a similar situation a while ago. It was a project in Ireland for a membership system for a cultural organisation and one of the requirements was that the system handle Irish names.
Irish names can be very complex with
O
,O'
,Ó
,Ó'
,Mac
,Mc'
,Mc
(all with or without spaces after) and in some cases names likeUa hÚigínn
(not a typo - the smallh
followed by upper caseÚ
is correct in Irish). As you can see, there are also accented characters (a, e, i, o and u can take acute accents). The problem is that many people use many different variants of these names with/without apostrophes, accents and alternative spellings.You can look on these like your differing spellings in your
some_value
field!What we did was to create a "shadow" (
some_value_search
) column and search on that! OnINSERT
orUPDATE
, we eliminated apostrophes, accents, hyphens and spaces (in the names) using aTRIGGER
. Obviously, your phrases need spaces, but you could turnperson's thing
intoPERSONS THING
andthing-thing
intoTHINGTHING
.Then you display your data using the original
some_value
field, but always search onsome_value_search
.This was in the days before computed/calculated/generated fields. But MySQL (as of 5.7.6) has them and you should be able to eliminate a lot/most/all of any trigger code using them.
Your (expression) above could be (REPLACE(REPLACE(field, '-', ''), ''', '') to eliminate spaces and hyphens. However, thanks to MySQL's lack of a
[TRANSLATE()][3]
function, the code for making a string all-capitals is horrible (26 nestedREPLACE
s!). If you need to do anything complex, there are alwaysTRIGGER
s, but the above solution is fairly standard nowadays!