Home > Software design >  Escape '_' in MySql Like using backslash or escape statement
Escape '_' in MySql Like using backslash or escape statement

Time:07-16

Can someone please explain what is difference between using a backslash and the escape statement to escape an underscore (_) ?

Why is the result different for below queries here?

select 'a_a' like 'a\_a';

1

select 'a_a' like 'a_a' escape '_';

0

CodePudding user response:

select 'a_a' like 'a_a' escape '_';

In this code the '_' is escaping char which escapes the next char 'a'.

select 'a_a' like 'a\_a',
       'a_a' like 'a_a' escape '_',
       'a_a' like 'a__a' escape '_';
'a_a' like 'a\_a' | 'a_a' like 'a_a' escape '_' | 'a_a' like 'a__a' escape '_'
----------------: | --------------------------: | ---------------------------:
                1 |                           0 |                            1

db<>fiddle here

  • Related