MySQL Forums
Forum List  »  Newbie

merge queries to make json
Posted by: Ricardo Neves Correia Santos
Date: December 17, 2019 01:00PM

Hi to all

I have 2 SELECTS's. In each one i consider two colums with a json result
Now I want to aggregate the result. How do I do it

To clear it better

________________________


SELECT pa.paper_id,json_arrayAGG( json_object( "id", a.author_id, "name", a.name) ) AS authors

FROM paper as p

JOIN paper_author as pa On p.paper_id=pa.paper_id
JOIN author as a On pa.author_id=a.author_id
Group by p.paper_id;
_______________________

results:

#paper_id, authors
1 [{"id": 1, "name": "Ric"}, {"id": 2, "name": "Joan"}]
2 [{"id": 2, "name": "Joan"}, {"id": 3, "name": "Ruth"}]
3 [{"id": 4, "name": "Ivan"}]


and

__________________

SELECT pt.paper_id,json_arrayAGG( json_object( "id", t.topic_id, "tag", t.tag) ) AS topics

FROM paper as p

JOIN paper_topic as pt On p.paper_id=pt.paper_id
JOIN topic as t On pt.topic_id=t.topic_id
Group by p.paper_id;
__________________

results:


# paper_id, topics
1, [{"id": 1, "tag": "tag1"}, {"id": 2, "tag": "tag2"}]
2, [{"id": 2, "tag": "tag2"}]
3, [{"id": 1, "tag": "tag1"}]


Now I want to do something like


SELECT json_object ("authors": column of first selection, "topics": column of second selection)


...

to get for each paper (3 rows)

{"authors":[column of first selection], "topics":[column of second selection]}


help is appreciated

Options: ReplyQuote


Subject
Written By
Posted
merge queries to make json
December 17, 2019 01:00PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.