How can we match the values having backslashes, like ‘a\\b’, from MySQL column?


With the help of an RLIKE operator, we can perform such kind of matching. The only concept is about to use a number of backslashes in MySQL query. The example below will make it clearer −

We have the following table having values such as ‘a\b’ and ‘a\b’.

mysql> select * from backslashes;
+------+-------+
| Id   | Value |
+------+-------+
|    1 | 200   |
|    2 | 300   |
|    4 | a\b  |
|    3 | a\b   |
+------+-------+
4 rows in set (0.10 sec)

Now suppose if we want to match the value ‘a\b’ then we need to write eight backslashes. It is because the second backslash is not escaped by the first so to compare two liters we need to double the backslash but as we are querying a table for such string from the MySQL string then this doubling happens twice-once in the client and once in the database. Hence we need to use four times backslashes as done in the queries below −

mysql> Select * from backslashes where value RLIKE 'a\\\\b';
+------+-------+
| Id   | Value |
+------+-------+
|    4 | a\b  |
+------+-------+
1 row in set (0.00 sec)

mysql> Select * from backslashes where value RLIKE 'a\\b';
+------+-------+
| Id   | Value |
+------+-------+
|    3 | a\b   |
+------+-------+
1 row in set (0.01 sec)

Updated on: 22-Jun-2020

615 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements