要求很简单,我需要获取其中恰好包含“4”的行,但问题是在 Sqllite 中没有 FIND_IN_SET 函数。我尝试使用 LIKE 查询获取值:
我尝试过的代码片段
select * from tbl_opportunity where (',' || opp_Business_id || ',') LIKE ',4,%'
但它仅返回 2 行,如果该行包含“24”或任何具有类似“4”数据的数字,有时还会返回更多行
Sqlite 确实支持正则表达式
WHERE opp_Business_id REGEXP "\b" || 4 || "\b";
- 否则您将需要匹配所有可能的组合。当 2/3 的示例记录失败时,您的模式与至少不匹配应该是显而易见的; WHERE (opp_Business_id == '4' OR opp_Business_id LIKE '%,4' OR opp_Business_id LIKE '4,%' OR opp_Business_id LIKE '%,4,%');
这将与 LIKE
进行四次比较 - 而与 REGEXP
只进行一次比较。
这个问题是关于Android平台的,
Android's Sqlite
不支持REGEXP
关键字。您必须像这样编写查询[我使用 Room 语法以便更好地理解]:
@Query(
"SELECT * FROM tbl_opportunity WHERE " +
"(" +
"',' || opp_Business_id || ',' LIKE '%' || :businessId || ',%' OR " +
"',' || opp_Business_id || ',' LIKE '%' || :businessId OR " +
"',' || opp_Business_id || ',' LIKE :businessId || '%'" +
") "
)
suspend fun getByBusinessId(businessId: Int): List<Opportunity>