Skip to content

Instantly share code, notes, and snippets.

@tobalsan
Created July 29, 2014 11:32
Show Gist options
  • Save tobalsan/0e7f12b27a98e50bb506 to your computer and use it in GitHub Desktop.
Save tobalsan/0e7f12b27a98e50bb506 to your computer and use it in GitHub Desktop.
SELECT
p.*,
GROUP_CONCAT(t.tag_name) tagged
FROM
tags_relation tr
INNER JOIN posts p ON p.post_id = tr.rel_post_id
INNER JOIN tags t ON t.tag_id = tr.rel_tag_id
GROUP BY
p.post_id
-- Other example
SELECT DATE_FORMAT(t.date, '%e/%m/%Y') as date, t.status, t.type, t.description, t.amount, o.name as other, GROUP_CONCAT(g.name) as tags FROM transactions t
LEFT JOIN others o ON t.other_id = o.id
LEFT JOIN transactions_tags tt ON t.id = tt.transaction_id
LEFT JOIN tags g ON g.id = tt.tag_id
WHERE t.account_id = ? GROUP BY t.id ORDER BY date DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment