[英]SQL SELECT from table when substring is in a string
Let's say I have a table named table1
in a sqlite3 database (via python) with two columns named 'id' and 'string' and here are the three entries in the table: 假设我在sqlite3数据库(通过python)中有一个名为
table1
的表,其中有两列分别名为“ id”和“ string”,这是表中的三个条目:
id string
'a' 'google'
'b' 'apple'
'c' 'application'
so let's say I want all the rows with 'app' in them, so how would i do that? 所以说我想要所有带有“ app”的行,那么我该怎么做? I'm looking for something like this:
我正在寻找这样的东西:
SELECT * FROM table1 WHERE 'app' in string
which would in theory return the last 2 rows.... 从理论上讲,这将返回最后两行...。
You can use LIKE
您可以使用
LIKE
SELECT * FROM table1
WHERE string LIKE '%app%'
UPDATE: 更新:
When you use a front wildcard ( LIKE '%app%'
), the performance will be roughly the same. 当您使用前置通配符(如
LIKE '%app%'
)时,性能将大致相同。
If you want string starting with app
, then using LIKE 'app%'
would be better. 如果要以
app
开头的字符串,则使用LIKE 'app%'
会更好。 Here's the reason :- 原因如下:-
The part before the first wildcard serves as an access predicate, that is, they limit the scanned index range.
第一个通配符之前的部分用作访问谓词,即它们限制了扫描索引范围。 The remaining characters do not narrow the scanned index range—non-matching entries are just left out of the result.
其余字符不会缩小扫描的索引范围-不匹配的条目仅被排除在结果之外。
尝试这个:
SELECT * FROM table1 WHERE string LIKE '%app%'
It's very simple. 非常简单 You need to use the LIKE '%' operator in SQL.
您需要在SQL中使用LIKE'%'运算符。 It's for string matching.
它用于字符串匹配。
Your query should be something like this: 您的查询应该是这样的:
SELECT * FROM table1
WHERE string LIKE '%app%'
Since you didn't mention whether 'app' should be positioned at the beginning of string or the end, I've wrapped the string 'app' around two '%', so that it will locate the 'app' string anywhere inside string. 由于您没有提到“ app”应该放置在字符串的开头还是结尾,因此我将字符串“ app”包装在两个“%”周围,以便它将“ app”字符串放置在字符串内的任何位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.