SQL 如何在 SQLite 中搜索子字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3671761/
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
How to search for a substring in SQLite?
提问by T.T.T.
Whats the most efficient way to search for a sub string in SQLite?
在 SQLite 中搜索子字符串的最有效方法是什么?
I'm looking at the LIKE operator.
我正在查看 LIKE 运算符。
Do I have the right idea? Has this worked well for you?
我有正确的想法吗?这对你有用吗?
http://www.sqlite.org/lang_expr.html
http://www.sqlite.org/lang_expr.html
Thank You.
谢谢你。
回答by GrandmasterB
Yepper, use Like. Select id from sometable where name like '%omm%'
would return any row that had 'omm' anywhere in the name column.
是的,使用 Like。 Select id from sometable where name like '%omm%'
将返回名称列中任何位置带有 'omm' 的任何行。
回答by Jerry Coffin
You canuse LIKE
, but it gets reallyslow if the pattern you're searching for starts with '%'
-- i.e., if the substring you're looking for isn't necessarily at the beginning of the field.
您可以使用LIKE
,但如果您要搜索的模式以 开头,它会变得非常慢'%'
——即,如果您要查找的子字符串不一定位于字段的开头。
If you need to do such searches, consider using FTS3, which makes full-text searching considerably more efficient.
如果您需要进行此类搜索,请考虑使用FTS3,这使得全文搜索效率更高。
回答by Alexei Martianov
Years have passed since the question was asked and answered and in 2012 SQLite version 3.7.15 introduced a function instr( string, substring)
- returns the location of a substring in a string, 0 if not found. (https://www.techonthenet.com/sqlite/functions/instr.php)
自从提出和回答问题以来已经过去了很多年,并且在 2012 年 SQLite 3.7.15 版引入了一个函数instr( string, substring)
- 返回字符串中子字符串的位置,如果找不到,则返回 0。( https://www.techonthenet.com/sqlite/functions/instr.php)
sqlite> SELECT instr('TechOnTheNet.com', 'T');
Result: 1
sqlite> SELECT instr('TechOnTheNet.com', 'T');
结果:1
I have not benchmarked against LIKE
, but IMHO could be faster.
我没有针对 进行基准测试LIKE
,但恕我直言可能会更快。