[英]What does \% and \\ mean in a SQL query?
I know the meaning of %
and _
wildcard characters ,but i was stuck in a question which was using the two additional characters \\%
and \\\\
,i was not able to understand what these characters actually mean in the SQL
query 我知道
%
和_
通配符的含义,但我陷入了一个问题,即使用了两个额外的字符\\%
和\\\\
,我无法理解这些字符在SQL
查询中的实际含义
SELECT productID
FROM productList
WHERE productName LIKE 'ab\%cd%'
and 和
SELECT productID
FROM productList
WHERE productName LIKE 'ab\\cd%'
are these two same things or different ?? 这两个相同或不同的东西?
Since %
is a special character, you have to escape it with a \\
to match a literal %
symbol in your data. 由于
%
是一个特殊字符,因此必须使用\\
来转义它以匹配数据中的文字%
符号。 So, 'ab\\%cd%'
matches the letter a, followed by the letter b, followed by a % symbol, the letter c, the letter d, then any other text (because the last %
is a wildcard). 因此,
'ab\\%cd%'
匹配字母a,后跟字母b,后跟%符号,字母c,字母d,然后是任何其他文本(因为最后一个%
是通配符)。
Similarly, since \\
is a special character used to create escape sequences, you have to escape it to match a literal \\
in a pattern, so to match a single \\
you have to encode it as \\\\
. 类似地,因为
\\
是用于创建转义序列的特殊字符,所以必须将其转义为匹配模式中的文字\\
,因此要匹配单个\\
您必须将其编码为\\\\
。
I believe the best way to see the difference is by example. 我相信看到差异的最好方法就是举例。
To better understand it you will need knowledge about 3 things when using LIKE
operator in SQL: 为了更好地理解它,在SQL中使用
LIKE
运算符时需要了解3件事:
\\
is used to escape special characters to use them as normal chars \\
用于转义特殊字符以将它们用作普通字符 %
is used to match any number of characters (including 0) %
用于匹配任意数量的字符(包括0) \\
and %
so if you want to include them literally you need to escape them, so to check for them in text column you respectively need to use \\\\
and \\%
. \\
和%
所以如果你想要逐字地包含它们你需要转义它们,所以要在文本列中检查它们你需要分别使用\\\\
和\\%
。 Below is a table with words and true/false results for LIKE
comparison with both patterns: 下面是一个表格,其中包含单词和true / false结果,以便与两种模式进行
LIKE
比较:
word | ab\%cd% | ab\\cd%
----------+---------+---------
ab\ | f | f -- this would match second pattern but there is no "cd" at the end
ab\cd | f | t -- \\ is escaped "\", and % matches none characters
ab\cdxzy | f | t -- \\ is escaped "\", and % matches character sequence "xzy"
abcd | f | f -- every string requires either "%" or "\" character after "ab"
ab%cd | t | f -- \% is escaped "%", and % matches none characters
ab%cdxzy | t | f -- \% is escaped "%", and % matches character sequence "xzy"
ab\%cd | f | f -- there is no pattern which matches both chars "\%" in sequence
ab%\cd | f | f -- same as above, but characters are "%\" in sequence
The
\\%
and\\_
sequences are used to search for literal instances of%
and_
in pattern-matching contexts where they would otherwise be interpreted as wildcard characters.\\%
和\\_
序列用于在模式匹配上下文中搜索%
和_
文字实例,否则它们将被解释为通配符。
For \\\\
it searches for a single back slash \\
. 对于
\\\\
它会搜索单个反斜杠\\
。
Ref: MySQL 8.0 Reference Manual, 9.1.1 String Literals, Table 9.1 Special Character Escape Sequences 参考: MySQL 8.0参考手册,9.1.1字符串文字,表9.1特殊字符转义序列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.