Why does “_” (underscore) match “-” (hyphen)?

后端 未结 2 1115
陌清茗
陌清茗 2020-12-07 14:16

I have to look for a PDF manual using this query:

root@localhost:test> select * from a where name like \'%taz_manual%.pdf%\';
+--------------------+------         


        
2条回答
  •  攒了一身酷
    2020-12-07 14:48

    Because the underscore _ is a wildcard like the percent %, except that it only looks for one character.

    SQL pattern matching enables you to use "_" to match any single character and "%" to match an arbitrary number of characters (including zero characters).

    (From section 3.3.4.7. Pattern Matching in the MySQL documentation.)

    If you want to use the underscore in like as a literal, you have to escape it:

    select * from a where name like '%taz\_manual%.pdf%';
    

提交回复
热议问题