SQL SQLite 喜欢 % 和 _
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7323162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLite Like % and _
提问by Francisc
I can't figure out what the underscore character does in an SQLite likestatement.
The wildcard character, %, is probably the same as in most other SQL databases.
我无法弄清楚 SQLitelike语句中下划线字符的作用。通配符%可能与大多数其他 SQL 数据库中的相同。
So, what does the damn _character do?
那么,该死的_角色是做什么的呢?
回答by Benoit
It is standard SQL that in LIKEexpressions:
LIKE表达式中的标准 SQL :
%matches any sequence of characters, including an empty one. It is equivalent to.*in a regular expression._matches a single character. It is equivalent to.in a regular expression.You can choose a character for escaping
%,_and itself itself with:... WHERE expr LIKE 'a_b%c\d\%\_' ESCAPE '\'This will match
a×b×××c\d%_ora×bc\d%_but notabc\d%_nora×b×××cd%_.
%匹配任何字符序列,包括空字符。它相当于.*在正则表达式中。_匹配单个字符。它相当于.在正则表达式中。您可以选择一个字符进行 escaping
%,_以及它本身:... WHERE expr LIKE 'a_b%c\d\%\_' ESCAPE '\'这将匹配
a×b×××c\d%_ora×bc\d%_但不匹配abc\d%_nora×b×××cd%_。
Additionnally with SQLite you have the GLOBkeyword which behaves exactly the same way, except that %becomes *and _becomes ?.
此外,对于 SQLite,您的GLOB关键字的行为方式完全相同,除了%变成*和_变成?.
回答by mu is too short
The underscore is also the same as in most other SQL databases and matches any single character (i.e. it is the same as .in a regular expression). From the fine manual:
下划线也与大多数其他 SQL 数据库中的相同并且匹配任何单个字符(即它与.正则表达式中的相同)。来自精美手册:
An underscore ("_") in the LIKE pattern matches any single character in the string.
LIKE 模式中的下划线 ("_") 匹配字符串中的任何单个字符。
For example:
例如:
-- The '_' matches the single 'c'
sqlite> select 'pancakes' like 'pan_akes';
1
-- This would need '__' to match the 'ca', only one '_' fails.
sqlite> select 'pancakes' like 'pan_kes';
0
-- '___' also fails, one too many '_'.
sqlite> select 'pancakes' like 'pan___kes';
0
And just to make sure the results make sense: SQLite uses zero and one for booleans.
并且只是为了确保结果有意义:SQLite对 booleans使用零和一。
回答by DuckMaestro
Addendum to @Benoit's answer:
@Benoit 回答的附录:
The ESCAPEapplies to the most recent LIKEexpression, not all LIKEexpressions. To escape all you must use ESCAPEmultiple times, such as below.
在ESCAPE适用于最近的LIKE表现,并不是所有的LIKE表情。要逃避所有,您必须ESCAPE多次使用,如下所示。
WHERE foo LIKE '%bar^%%' ESCAPE '^' AND foo LIKE '%baz^_%' ESCAPE '^'
This predicate matches values of foowhich contain bar%, or bazplus any character.
此谓词匹配foo包含bar%或baz加任何字符的值。
回答by Jan ATAC
For the record, I use in XCode/Objective-C environment, '\' doesn't work. Use anything else instead...
作为记录,我在 XCode/Objective-C 环境中使用,“\”不起作用。改用其他任何东西...
C-style escapes using the backslash character are not supported because they are not standard SQL (https://www.sqlite.org/lang_expr.html)
不支持使用反斜杠字符的 C 样式转义,因为它们不是标准 SQL ( https://www.sqlite.org/lang_expr.html)

