Skip to content

Instantly share code, notes, and snippets.

@khairulhasanmd
Created April 4, 2024 05:56
Show Gist options
  • Save khairulhasanmd/e36525b8dfc3e7649049b0bca52c0f37 to your computer and use it in GitHub Desktop.
Save khairulhasanmd/e36525b8dfc3e7649049b0bca52c0f37 to your computer and use it in GitHub Desktop.
Mysql Get child table data as json in same row
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