Operator Behaviors
Equal Operator =
If you are doing an exact searches using the equal operator =
the solution from @rsanchez
is correct. For instance, finding row equal to œ
can be done using this exact value œ
or its transliterated value oe
:
SELECT 'œ' = 'oe' COLLATE utf8_unicode_ci ;
-- true
+-------------------------------------+
| 'œ' = 'oe' COLLATE utf8_unicode_ci |
+-------------------------------------+
| 1 |
+-------------------------------------+
Will return rows being equal to œ
or oe
if user use any of them.
LIKE
Operator
However if you want to do fuzzy search using the LIKE
operator, this won't work. For instance, searching for words containing œ
by typing oe
will not return row with œ
.
SELECT 'œ' LIKE 'oe' COLLATE utf8_unicode_ci ;
-- false
+----------------------------------------+
| 'œ' LIKE 'oe' COLLATE utf8_unicode_ci |
+----------------------------------------+
| 0 |
+----------------------------------------+
Explanation ?
This is not a bug but the expected behavior by SQL standard (see Alexander Barkov answer) as LIKE
is doing comparison on a character basis while =
use a more complex approach (e.g. allowing transliteration equality).
Solutions
Provide the transliterated form
A solution would be to generate the transliteration of your search and modify your query to search for each form:
SELECT * FROM `mytable`
WHERE `myfield` LIKE 'oe' or `myfield` LIKE 'oe'
COLLATE utf8_unicode_ci;
Use FullText search
Since MySQL 5.6, fulltext search is now available for InnoDB (previously it was only available for MyISAM
tables).