I have problem with that more comments the comment table has. Then the more the User
table repeating it value in the report group. It should only fetch user once, while comments as many there is which it is the current behaviour. But I would like the user being fetched once.
How it's now:
["text": "My first report",
user: [{"display_name": "Cyclops", "photo_url": "CYCLOPS_IMAGE.png",
{"display_name": "Cyclops", "photo_url": "CYCLOPS_IMAGE.png"},
{"display_name": "Cyclops", "photo_url": "CYCLOPS_IMAGE.png"}}],
comments: [{"text": "Great Report", display_name: "Xavier"},
{"text": "Bad Report", display_name: "Logan"},
{"text": "Thanks for the feedback", display_name: "Cyclops"}]]
What i expect:
["text": "My first report",
user: [{"display_name": "Cyclops", "photo_url": "CYCLOPS_IMAGE.png"}],
comments: [{"text": "Great Report", display_name: "Xavier"},
{"text": "Bad Report", display_name: "Logan"},
{"text": "Thanks for the feedback", display_name: "Cyclops"}]]
Code:
SELECT report.text,
Json_arrayagg(Json_object('display_name', users.display_name, 'photo_url', users.photo_url)) AS USER,
Json_arrayagg(Json_object('text', report_comments.text, 'display_name', report_comments.user_id)) AS COMMENTS
FROM report
INNER JOIN users
ON users.id = report.user_id
LEFT JOIN report_comments
ON report_comments.report_id = report.id
WHERE report.user_id = :userId
GROUP BY report.id
question from:
https://stackoverflow.com/questions/65545515/mysql-inner-join-and-group-by-repeating-row 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…