简体   繁体   English

使用 JSON1 跨多个表聚合 SQLite 查询

[英]Aggregate SQLite query across multiple tables using JSON1

I can't get my head around the following problem.我无法解决以下问题。 The other day I learned how to use the JSON1 family of functions, but this time it seems to be more of an SQL issue.前几天我学习了如何使用 JSON1 系列函数,但这一次似乎更像是一个 SQL 问题。

This is my database setup:这是我的数据库设置:

CREATE TABLE persons(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE)
CREATE TABLE interests(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE)
CREATE TABLE persons_interests(person INTEGER, interest INTEGER, FOREIGN KEY(person) REFERENCES persons(id), FOREIGN KEY(interest) REFERENCES interests(id))

INSERT INTO persons(name) VALUES('John')
INSERT INTO persons(name) VALUES('Jane')

INSERT INTO interests(name) VALUES('Cooking')
INSERT INTO interests(name) VALUES('Gardening')
INSERT INTO interests(name) VALUES('Relaxing')

INSERT INTO persons_interests VALUES(1, 1)
INSERT INTO persons_interests VALUES(1, 2)
INSERT INTO persons_interests VALUES(2, 3)

Based on this data I'd like to get the following output, which is all interests of all persons aggregated into a single JSON array:根据这些数据,我想得到以下 output,这是所有人的所有兴趣,聚合到一个 JSON 数组中:

[{name: John, interests:[{name: Cooking},{name: Gardening}]}, {name: Jane, interests:[{name: Relaxing}]}]

Now the following is what I tried to do.现在以下是我尝试做的。 Needless to say, this doesn't give me what I want:不用说,这并没有给我想要的东西:

SELECT p.name, json_object('interests', json_group_array(json_object('name', i.name))) interests
FROM persons p, interests i
JOIN persons_interests pi ON pi.person = p.id AND pi.interest = i.id

The undesired output is:不需要的 output 是:

John|{"interests":[{"name":"Cooking"},{"name":"Gardening"},{"name":"Relaxing"}]}

Any help is highly appreciated!非常感谢任何帮助!

For using json_group_array you must group line, in your case by person, except you want only one row with all your results.对于使用json_group_array ,您必须按人对行进行分组,除非您只希望一行包含所有结果。

Example 1)示例 1)

This first version, will give you 1 json object by person, so the result will be N rows for N persons:第一个版本将按人为您提供 1 json object,因此结果将为 N 人的 N 行:

SELECT json_object( 'name ',
                    p.name, 
                    'interests', 
                    json_group_array(json_object('name', i.name))) jsobjects
FROM persons p, interests i
JOIN persons_interests pi ON pi.person = p.id AND pi.interest = i.id
group by p.id ;

Example 2)例 2)

This second version, will give return 1 big json array that contains all persons, but you fetch only one row.第二个版本将返回 1 个包含所有人的大 json 数组,但您只获取一行。

SELECT json_group_array(jsobjects) 
FROM (

    SELECT json_object( 'name ',
                        p.name, 
                        'interests', 
                        json_group_array(json_object('name', i.name))) jsobjects
    FROM persons p, interests i
    JOIN persons_interests pi ON pi.person = p.id AND pi.interest = i.id
    group by p.id 
) jo ;

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

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