Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Mysql Nested json arrays

I Have trouble to achive nested json array to the desired result

Current Code:

SELECT Json_arrayagg(Json_object('report_id', rc.report_id, 'user_id', 
                     rc.user_id, 
                            'text', rc.text, 
                            'created', rc.created,
                            'user', Json_object(
                                         'display_name', 
                                          u.display_name, 
                                          'photo_url', u.photo_url)
)) AS comments 
FROM   report_comments rc 
       JOIN users u 
         ON rc.user_id = u.id 
WHERE ` report_id ` = :reportId 

Current result:

[{comments: [{text: My first comment, user: {photo_url: www.example.com, display_name: Jay Jay}, created: 2021-01-03 00:47:58.000000, user_id: 2, report_id: 5}]}]

What i try to get:

{comments: [{text: My first comment, user: {photo_url: www.example.com, display_name: Jay Jay}, created: 2021-01-03 00:47:58.000000, user_id: 2, report_id: 5}, {text: My second comment, user: {photo_url: www.example.com, display_name: Jay Jay}, created: 2021-03-06 00:17:38.000000, user_id: 2, report_id: 5}]}

I would like to remove [] at start, so i could access the array by doing row["comments"] instead of row[0]["comments"]


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

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...