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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:13:43  来源:igfitidea点击:

SQLite string contains other string query

sqlsqlite

提问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 LIKEis 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: instris 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 LIKEis case-insensitive, whereas instris case-sensitive.

另外,请记住,LIKE是区分大小写的,而instr是区分大小写