1
mysql> select 'ab' like 'a_%';
+-----------------+
| 'ab' like 'a_%' |
+-----------------+
|               1 |
+-----------------+
1 row in set (0.00 sec)

mysql> select 'ab' like 'a\_%';
+------------------+
| 'ab' like 'a\_%' |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

mysql> select 'a_b' like 'a\_%';
+-------------------+
| 'a_b' like 'a\_%' |
+-------------------+
|                 1 |
+-------------------+
1 row in set (0.00 sec)

Why do I need to escape _?

1
  • 3
    A Google search for "mysql like underscore" would have found you the information you desired. (Even just "mysql underscore".) Dec 18, 2011 at 14:11

2 Answers 2

9

Because _ and % are both special characters for LIKE.

_ matches one character, % any number of characters.

0
"_" means underscore. I think "_" used for hiding any character under it. It is special because we can place character for it.

You can get more from these links:

link1

link2

link3

link4

link5

link6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.