Compare two lists, one of them JSON, in SQLite - Stack Overflow

时间: 2025-01-06 admin 业界

I am trying to implement tagging on objects, tracked through an SQLite database. My table looks something like this (simplified for example):

create table file (
    path  varchar            not null,
    frontmatter  JSON    default '' not null, --JSON
    constraint page_pk
        primary key (logicalPath, ext)
);

The JSON field is structured like (again, simplified):

{
  "title": "Something",
  "tags": ["a", "b", "c"]
}

I'm trying to figure out the best way to do "find all documents with at least one of these tags" and "find all documents with all of these tags."

So far, I've figured out how to do that if I skip JSON and materialize the tags out to a separate table. It's just kind of ugly. With JSON, the best I've figured out how to do is use a View or CTE (WITH statment) and json_each() that produces that materialized table on the fly, which doesn't seem like much of a win.

I feel like there should be some way to intersect two list-fields, so I can do something like json_extract(frontmatter, '$.tags') IN (?, ?) or some "all" equivalent, but I've not been able to find one and my search-fu has not yet found anyone else doing it. Can someone offer pointers to how I would do this?

最新文章