SQL SQLite 字符串包含其他字符串查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3498844/
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 string contains other string query
提问by Joren
How do I do this?
我该怎么做呢?
For example, if my column is "cats,dogs,birds" and I want to get any rows where column contains cats?
例如,如果我的列是“cats,dogs,birds”并且我想获取列包含猫的任何行?
回答by OMG Ponies
Using LIKE:
使用喜欢:
SELECT *
FROM TABLE
WHERE column LIKE '%cats%' --case-insensitive
回答by Sam
While LIKE
is suitable for this case, a more general purpose solution is to use instr
, which doesn't require characters in the search string to be escaped. Note: instr
is available starting from Sqlite 3.7.15.
虽然LIKE
适用于这种情况,但更通用的解决方案是使用instr
,它不需要对搜索字符串中的字符进行转义。注意:instr
从 Sqlite 3.7.15 开始可用。
SELECT *
FROM TABLE
WHERE instr(column, 'cats') > 0;
Also, keep in mind that LIKE
is case-insensitive, whereas instr
is case-sensitive.
另外,请记住,LIKE
是区分大小写的,而instr
是区分大小写。