为什么"_" (下划线)匹配“-" (连字符)? [英] Why does "_" (underscore) match "-" (hyphen)?

查看:128
本文介绍了为什么"_" (下划线)匹配“-" (连字符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用此查询来查找PDF手册:

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

root@localhost:test> select * from a where name like '%taz_manual%.pdf%';
+--------------------+------------------+-------------+
| name               | description      |        size | 
+--------------------+------------------+-------------+
| taz-manual-1.1.pdf | Manual v1.0 TA-Z |    31351902 |
| taz-manual-0.2.pdf | Manual v1.0 T1-A |     3578278 |
| taz_manual-2.0.pdf | Manual v2.0 GA-X |   542578278 |
etc........
+--------------------+------------------+-------------+
132 row in set (0.00 sec)

为什么我将名称指定为taz_manual%.pdf时会看到带有短划线的那个?

Why am I seeing the the one with dashes when I specify the name to be taz_manual%.pdf?

推荐答案

因为下划线_是通配符,如百分比%,只是它只查找一个字符.

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

SQL模式匹配使您可以使用"_"来匹配任何单个字符,并使用%"来匹配任意数量的字符(包括零个字符).

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

(从 3.3.4.7.模式匹配在MySQL文档中.)

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

如果要将like中的下划线用作文字,则必须对其进行转义:

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%';

这篇关于为什么"_" (下划线)匹配“-" (连字符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆