It works for me with 6 backslashes when using the left wildcard:
mysql> SELECT * FROM `tmptest` WHERE `a` LIKE '%\\\\\\n%'; +-----------------+ | a | +-----------------+ | \n | | \n\ndfsdfsdfs\n | +-----------------+ 2 rows in set (0.00 sec)
Using mysqld Ver 5.1.49
@Karolis, as I understand it, the expression for the LIKE operator must be parsed twice, so \\\\ goes to \ when used with LIKE.
But how to explain it (using the expression "back \ slash"):
SELECT 'back\\slash' LIKE '%back\\\\slash%'; β TRUE (normal behavior)
SELECT 'back\\slash' LIKE '%back\\\\\slash%'; β TRUE (5 backslashes?)
SELECT 'back\\slash' LIKE '%back\\\\\\slash%'; β TRUE (6 backslashes?)
SELECT 'back\\slash' LIKE '%back\\\\\\\slash%'; β TRUE (7 backslashes?)
SELECT 'back\\slash' LIKE '%back\\\\\\\\slash%'; β FALSE (normal behavior, I think ..)
However, if you are only looking for "\":
mysql> SELECT 'back\\slash' LIKE '%\\\\%'; β FALSE (but should work)
mysql> SELECT 'back\\slash' LIKE '%\\\\\%'; β TRUE (5 backslashes)
mysql> SELECT 'back\\slash' LIKE '%\\\\\\%'; β TRUE (6 backslashes)
mysql> SELECT 'back\\slash' LIKE '%\\\\\\\%'; β FALSE (7 backslashes)
In this particular question, you can use another escape character | and generally bypass the problem (if the | character is not found):
mysql> SELECT 'back\\slash' LIKE '%\\%' ESCAPE '|'; β TRUE
So maybe some mysql guru there can explain it. I just can not. also tested with mysql 5.1.53 on another machine. The same behavior was observed. As I started with the comments, this is a pretty interesting question.