Created
April 4, 2024 05:56
-
-
Save khairulhasanmd/e36525b8dfc3e7649049b0bca52c0f37 to your computer and use it in GitHub Desktop.
Mysql Get child table data as json in same row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT a.id, a.product_name, a.pdf_url, | |
JSON_ARRAYAGG( | |
JSON_OBJECT( | |
'id', c.id, | |
'name', c.name | |
) | |
) AS tag | |
FROM products a | |
LEFT JOIN product_tags b ON (a.id = b.product_id) | |
LEFT JOIN tags c ON (b.tag_id = c.id) | |
GROUP BY a.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment