0

Two tables. Post and Tag. Many to Many bi-directional relationship.

Post Table              Tag Table             Post_Tag Pivot Table
---------------         ------------          -----------------
id   content            id   name             id_post   id_tag
---------------         ------------          -----------------
 1   hey there           1    php                1        1
                         2    python             1        2

I want to get all posts with their tags.

I've tried this:

SELECT P.title, T.tag
From Post P
JOIN Tag T
ORDER BY P.id DESC

It works, but it gives duplicates:

hey there, php
hey there, python

Is there a way to group tags together in one row here. Thanks

1 Answer 1

0

You didn't provide DDL and sample data so we can't test anything. Also, it is not clear what is the join predicate, but I think you are looking for GROUP_CONCAT, something along the lines of (Pseudo code):

SELECT P.Title, GROUP_CONCAT(T.Tag SEPARATOR ',') AS Concatenated_Tags
FROM Post P INNER JOIN Tag T ...
GROUP BY P.Title;

HTH

1
  • thanks that's what I was looking for, though I need to now somehow make GROUP_CONCAT work with doctrine n i'll be all set. thanks for your help Dec 14, 2018 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.