简体   繁体   English

在sqlite中使用json1查询多个值的JSON数组

[英]Query a JSON array for multiple values using json1 in sqlite

I am trying to use the json1 extension to query my sqlite database and want to query a json array to match multiple values and return the entry that matches all the values.我正在尝试使用 json1 扩展来查询我的 sqlite 数据库,并希望查询一个 json 数组以匹配多个值并返回匹配所有值的条目。

Example entities:示例实体:

Entity 1:实体 1:

{
    EntityID: 123,
    tags: [tag1, tag2, tag3]
}

Entity 2:实体 2:

{
    EntityID: 234,
    tags: [tag2, tag3]
}

In this example I want to query for tags that match multiple values.在这个例子中,我想查询匹配多个值的标签。 If my input is [tag1, tag2] then I want to return only Entity 1 whereas if my input is tag2 then I want to return both the entities.如果我的输入是[tag1, tag2]那么我只想返回Entity 1而如果我的输入是tag2那么我想返回两个实体。

The query I expected to work was我希望工作的查询是

SELECT * 
FROM entities, json_each(tags) 
WHERE json_each.value IS "tag1" 
    AND json_each.value IS "tag2"

But this query fails all the cases.但是这个查询在所有情况下都失败了。 As to why it fails, I think that it tries to match each value of the array to be tag1 and tag2 which is not the case as each element has only 1 value.至于它为什么失败,我认为它试图将数组的每个值匹配为tag1tag2 ,但情况并非如此,因为每个元素只有 1 个值。 Is this valid or is there another explanation?这是有效的还是有其他解释?

A query that did work for me in the very specific use case of checking [tag1, tag2] values and returning only Entity 1 is在检查[tag1, tag2]值并仅返回Entity 1的非常具体的用例中,对我有用的查询是

SELECT * 
FROM entities, json_each(tags) 
WHERE json_each.value IS "tag1" OR "tag2"

I want to know as to why it works in the case above?我想知道为什么它在上述情况下有效? The only thing that I can think of is that it checks each value of the array to be either tag1 or tag2 but I don't understand why it returns Entity 1 which also has a value of tag3 and does not match the criteria.我唯一能想到的是它检查数组的每个值是tag1还是tag2但我不明白为什么它返回Entity 1 ,它的值也为tag3并且不符合条件。 This case however fails in most other cases for example where I search [tag3, tag4] , it returns both the entities in this case but it should return none.然而,这种情况在大多数其他情况下都失败了,例如在我搜索[tag3, tag4]情况下,它在这种情况下返回两个实体,但它应该没有返回。 So this is definitely not the answer所以这绝对不是答案

I still have not been able to find the right way to query this efficiently and am looking for any suggestions.我仍然无法找到有效查询的正确方法,并且正在寻找任何建议。

Ideally if there is an even simpler way to query this then that would be much appreciated.理想情况下,如果有一种更简单的方法来查询,那将不胜感激。 What I have in mind is something where I give the query a list of values for the tags and it matches all of them without me having to use AND / OR operators我想到的是,我为查询提供了一个标签值列表,它可以匹配所有标签,而我不必使用AND / OR运算符

The first query fetches tag1 first, compares it with tag1 and tag2, fails and then fetches another value.第一个查询首先获取 tag1,将它与 tag1 和 tag2 进行比较,失败然后获取另一个值。 It is failing because the value cant be tag1 and tag2 at the same time.它失败是因为值不能同时是 tag1 和 tag2。

I would suggest using nested queries, replacing the from clause with another query我建议使用嵌套查询,用另一个查询替换 from 子句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM