\(反斜杠)在 SQL 查询中是什么意思? [英] What does \ (backslash) mean in an SQL query?
问题描述
我有以下查询
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%'
这会导致答案包含任何字符+a+\+ 吗?
will that result in answers that have any char+a+\+whatever?
Pa\pe 之类的东西是否有效?
is something like Pa\pe valid as a result?
Ca% 或 _a% 可能是有效答案吗?
are Ca% or _a% valid answers maybe?
\ 在 SQL 查询中如何正常运行??
how does \ behave normally inside an SQL query??
推荐答案
% 是匹配 LIKE 子句中零个或多个字符的通配符._ 是一个通配符,它恰好匹配 LIKE 子句中的一个字符.
% is a wildcard character that matches zero or more characters in a LIKE clause. _ is a wildcard character that maches exactly one character in a LIKE clause.
\ 是一种特殊字符,称为转义字符,表示应按字面解释紧跟其后的字符(用于单引号、通配符等).
\ is a special character known as an escape character that indicates that the character directly following it should be interpreted literally (useful for single quotes, wildcard characters, etc.).
例如:
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a%'
将选择 txt1 值为 'xa1'、'xa taco'、'ya nothing' 等的记录
will select records with txt1 values of 'xa1', 'xa taco', 'ya anything really', etc.
现在假设您要实际搜索百分号.为此,您需要一个特殊字符来指示 % 不应 被视为通配符.例如:
Now let's say you want to actually search for the percent sign. In order to do this you need a special character that indicates % should not be treated as a wildcard. For example:
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%'
将选择 txt1 值为 'ba%' 的记录(但没有别的).
will select records with txt1 values of 'ba%' (but nothing else).
最后,LIKE 子句通常会包含通配符(否则您可以只使用 = 而不是 LIKE).因此,您可能会看到包含 \%% 的查询.这里第一个百分号将被视为文字百分号,但第二个将被解释为通配符.例如:
Finally, a LIKE clause would typically contain a wildcard (otherwise you could just use = instead of LIKE). So you might see a query containing \%%. Here the first percent sign would be treated as a literal percent sign, but the second would be interpreted as a wildcard. For example:
SELECT txt1 FROM T1 WHERE txt1 LIKE '_a\%%'
将选择 txt1 值为 'da%something else'、'fa% taco'、'ma% bundle of tacos' 等的记录
will select records with txt1 values of 'da%something else', 'fa% taco', 'ma% bunch of tacos', etc.
这篇关于\(反斜杠)在 SQL 查询中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!