Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

node.js - how to obtain objects in objects (with all the key names) in SQL?

my database contains two tables named category and sport:

category
-----------------------------------
id        |   title    | ...
1         |   summer
2         |   autumn
3         |   spring
4         |   winter

sport
--------------------------------------------------------------
id        |   title       | description | category_id  | ...
1         |   ski         | ...         |   4
2         |   surf        | ...         |   1
3         |   snorkeling  | ...         |   1
4         |   running     | ...         |   3
5         |   hiking      | ...         |   2
...

my question is how to obtain with one request in Sql (postgresql) this result :

{
        category: [
                {
                        c.id: 1,
                        c.title: summer,
                        sport: [
                                {
                                        s.id: 2,
                                        s.title: surf,
                                },
                                {
                                        s.id: 3,
                                        s.title: snorkeling,
                                },
                        ],     
                },
                {
                        c.id: 2,
                        c.title: autumn,
                        sport: [
                                {
                                        s.id: 5,
                                        s.title: hiking
                                }
                        ],
                },
                {
                        c.id: 3
                        c.title: spring,
                        sport: [
                                {
                                        s.id: 4,
                                        s.title: running,
                                }
                        ],
                },
                {
                        ...
                }               
        ]
}

I've tried with ARRAY_AGG but it removes the key_names and i need it to call the values with in my API.

question from:https://stackoverflow.com/questions/65872738/how-to-obtain-objects-in-objects-with-all-the-key-names-in-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to combine jsonb_agg(), to_jsonb()andjsonb_build_object()`

select to_jsonb(c)||jsonb_build_object('sport', jsonb_agg(to_jsonb(s) - 'category_id'))
from category c
  join sport s on s.category_id = c.id
group by c.id;

The above assumes that category.id is defined as the primary key.

It returns one row per category.

If you really want one gigantic array of all rows, you need to aggregate in two steps:

select jsonb_build_object('category', jsonb_agg(to_jsonb(c)||sport))
from category c
   join (
    select category_id, jsonb_build_object('sport', jsonb_agg(to_jsonb(s) - 'category_id')) as sport
    from sport s
    group by s.category_id
  ) s on s.category_id = c.id

Online example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...