最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a method to check if an array includes one value in SQLite? - Stack Overflow

programmeradmin2浏览0评论

Let's say, we have this SQLite table with id=number and tags=text:

| id   | tags                |  
| ---- | ------------------- |   
| 1    | ["love","sadness"]  |    
| 2    | ["love"]            |   
| 3    | ["happiness","joy"] |

Is there a way to only return the rows which their cells tags include "love" for example, like this command in MySQL : SELECT * from my_table WHERE JSON_CONTAINS(tags, '"love"') in SQLite.

I use this with the library sql.js wtih node.js

Let's say, we have this SQLite table with id=number and tags=text:

| id   | tags                |  
| ---- | ------------------- |   
| 1    | ["love","sadness"]  |    
| 2    | ["love"]            |   
| 3    | ["happiness","joy"] |

Is there a way to only return the rows which their cells tags include "love" for example, like this command in MySQL : SELECT * from my_table WHERE JSON_CONTAINS(tags, '"love"') in SQLite.

I use this with the library sql.js wtih node.js

Share Improve this question edited Mar 7, 2021 at 20:28 oz123 28.9k29 gold badges132 silver badges194 bronze badges asked Aug 29, 2020 at 21:23 user10458813user10458813
Add a comment  | 

2 Answers 2

Reset to default 18

If your version of sqlite has the JSON1 extension compiled in:

SELECT *
FROM my_table
WHERE EXISTS (SELECT 1 FROM json_each(tags) WHERE value = 'love')
ORDER BY id;

will return

id  tags               
--  -------------------
1   ["love","sadness"] 
2   ["love"]           

I believe this should work:

SELECT * from my_table WHERE tags LIKE '%"love"%';

Keyword LIKE lets you query partial information in the column and it's used in WHERE clause just like operators =, IN, BETWEEN

Note:

  • The percent sign % wildcard matches any sequence of zero or more characters.
  • The underscore _ wildcard matches any single character.
发布评论

评论列表(0)

  1. 暂无评论