This is a spin-off from:
The following can be used to do a case-insensitive search for the specified value with the query builder:
->condition('foo', $foo, 'LIKE')
However, if $foo contains a wildcard character,
_ we need to escape these characters. In MySQL, we can do this by inserting a backslash in front of the wildcard character, e.g. using
addcslashes($foo, '\%_'). AFAICT SQL-92 says that there is no escape character and that backslash should be treated like any other character, but this is only enforced in the NO_BACKSLASH_ESCAPES mode.
In order to ensure consistent behaviour across databases we need to explicitly specify an escape character in our SQL, i.e. like this:
SELECT * FROM bar WHERE foo LIKE 'abc\\_def' ESCAPE '\\'
Though any character can be used, I suggest using backslash as this is the default on MySQL and PostgreSQL.
This patch has only been tested with MySQL.