Backslashes in data

from the Artful Common Queries page

Backslashes multiply weirdly:
SELECT 'a\b' RLIKE 'a\b'; 
returns 1, as does...
SELECT 'a\\b' RLIKE 'a\\\\b'; 
because in a pair of backslashes, the second is not escaped by the first, so to compare two literals you double each backslash in the RLIKE argument. But if you are querying a table for such a string from the MySQL client, this doubling happens twice--once in the client, and once in the database--so to find a column value matching 'a\\b', you need to write...
SELECT desc FROM xxx WHERE desc RLIKE 'aa\\\\\\\\bb';
That's eight backslashes to match two!

Last updated 22 May 2009