MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

Matt Shultz

I have a normalized database and I'm trying to return data from multiple tables using JOINs and GROUP_CONCAT.

Problem: Rows are being duplicated with GROUP_CONCAT. I can't use DISTINCT because some of the data (ingredient mfr) does need to be duplicated.

Here is my current query and db structure (SQL Fiddle):

SELECT recipe.*, 
GROUP_CONCAT(recipe_detail.ingredient_id) AS iid,  
GROUP_CONCAT(ingredient.name) AS iname, 
GROUP_CONCAT(ingredient_mfr.abbr) AS mabbr, 
GROUP_CONCAT(recipe_tag.name) AS tag
FROM  recipe
LEFT JOIN recipe_detail
    ON recipe.id = recipe_detail.recipe_id
LEFT JOIN ingredient
    ON recipe_detail.ingredient_id = ingredient.id
LEFT JOIN ingredient_mfr
    ON ingredient.mfr_id = ingredient_mfr.id
LEFT JOIN recipe_tagmap
    ON recipe.id = recipe_tagmap.recipe_id
LEFT JOIN recipe_tag
    ON recipe_tagmap.tag_id = recipe_tag.id
WHERE recipe.user_id = 1
GROUP BY recipe.id

recipe
+------------+------------+-----------+
|    id      |    name    |  user_id  |
+============+============+===========+
|     1      |  Test123   |     1     |
+------------+------------+-----------+
|     2      |  Test456   |     1     |
+------------+------------+-----------+
|     3      |  Test789   |     1     |
+------------+------------+-----------+

recipe_detail
+------------+---------------+
| recipe_id  | ingredient_id |
+============+===============+
|     1      |      193      |
+------------+---------------+
|     1      |      194      |
+------------+---------------+
|     2      |       16      |
+------------+---------------+
|     3      |      277      |
+------------+---------------+

ingredient
+------------+---------------+---------+
|     id     |      name     |  mfr_id |
+============+===============+=========+
|     16     |       Gin     |    4    |
+------------+---------------+---------+
|     193    |       Fig     |    3    |
+------------+---------------+---------+
|     194    |       Tea     |    3    |
+------------+---------------+---------+
|     277    |       Nut     |    2    |
+------------+---------------+---------+

ingredient_mfr
+------------+------------+
|    id      |    abbr    |
+============+============+
|     2      |    TFA     |
+------------+------------+
|     3      |    FA      |
+------------+------------+
|     4      |    LOR     |
+------------+------------+

recipe_tag
+------------+------------+
|    id      |    name    |
+============+============+
|     1      |    one     |
+------------+------------+
|     2      |    two     |
+------------+------------+
|     3      |    three   |
+------------+------------+
|     4      |    four    |
+------------+------------+
|     5      |    five    |
+------------+------------+
|     6      |    six     |
+------------+------------+
|     7      |    seven   |
+------------+------------+
|     8      |    eight   |
+------------+------------+
|     9      |    nine    |
+------------+------------+

recipe_tagmap
+------------+---------------+---------+
|     id     |   recipe_id   |  tag_id |
+============+===============+=========+
|     1      |       1       |    1    |
+------------+---------------+---------+
|     2      |       1       |    2    |
+------------+---------------+---------+
|     3      |       1       |    3    |
+------------+---------------+---------+
|     4      |       2       |    4    |
+------------+---------------+---------+
|     5      |       2       |    5    |
+------------+---------------+---------+
|     6      |       2       |    6    |
+------------+---------------+---------+
|     7      |       3       |    7    |
+------------+---------------+---------+
|     8      |       3       |    8    |
+------------+---------------+---------+
|     9      |       3       |    9    |
+------------+---------------+---------+

With my current query, my results look like this:

+------+---------+--------------+----------- ----+---------------+------------------+
|  id  |  name   |      iid     |     iname      |    mabbr      |       tag        |
+======+=========+==============+================+===============+==================+
|   1  | Test123 | 193,193,193, | Fig, Fig, Fig, | FA, FA, FA,   | one, two, three, |
|      |         | 194,194,194  | Tea, Tea, Tea  | FA, FA, FA    | one, two, three  |
+------+---------+--------------+----------------+---------------+------------------+
|   2  | Test456 | 16,16,16     | Gin, Gin, Gin  | LOR, LOR, LOR | four, five six   |
+------+---------+--------------+----------------+---------------+------------------+
|   3  | Test789 | 277,277,277  | Nut, Nut, Nut  | TFA, TFA, TFA | seven,eight,nine |
+------+---------+--------------+----------------+---------------+------------------+

What I would like my results to look like:

+------+---------+--------------+----------- ----+---------------+------------------+
|  id  |  name   |      iid     |     iname      |    mabbr      |       tag        |
+======+=========+==============+================+===============+==================+
|   1  | Test123 |   193, 194   |    Fig, Tea    |    FA, FA     | one, two, three, |
+------+---------+--------------+----------------+---------------+------------------+
|   2  | Test456 |      16      |      Gin       |     LOR       | four, five six   |
+------+---------+--------------+----------------+---------------+------------------+
|   3  | Test789 |     277      |      Nut       |     TFA       | seven,eight,nine |
+------+---------+--------------+----------------+---------------+------------------+

As you can see, the presence of multiple tags causes the ingredient data to duplicate. The presence of multiple ingredients causes the tags to duplicate. I have tried to use DISTINCT, but sometimes I will have multiple ingredients and each one of those will return it's own "mabbr", which may be the same it's other ingredient (see first row of expected results). Using DISTINCT, it will only return one instance of that "mabbr".

Is there a change I can make to my query to achieve what I'd like to do?

SQL Fiddle

transilvlad

You can resolve this by extracting the tag grouping to its own subquery:

SELECT
    recipe.*,
    GROUP_CONCAT(recipe_detail.ingredient_id) AS iid,
    GROUP_CONCAT(ingredient.name) AS iname,
    GROUP_CONCAT(ingredient_mfr.abbr) AS mabbr,
    (
      SELECT GROUP_CONCAT(recipe_tag.name)
        FROM recipe_tag
          INNER JOIN recipe_tagmap
            ON recipe_tagmap.tag_id = recipe_tag.id
        WHERE recipe_tagmap.recipe_id = recipe.id
     ) AS tag

  FROM recipe
    LEFT JOIN recipe_detail
      ON recipe.id = recipe_detail.recipe_id
    LEFT JOIN ingredient
      ON recipe_detail.ingredient_id = ingredient.id
    LEFT JOIN ingredient_mfr
      ON ingredient.mfr_id = ingredient_mfr.id

  WHERE recipe.user_id = 1
  GROUP BY recipe.id

(example fiddle)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

From Dev

MySQL GROUP_CONCAT(DISTINCT ...) ignores data

From Dev

SQL left join and group_concat returns duplicate data

From Dev

MySQL use GROUP_CONCAT when INSERTing data

From Dev

mysql GROUP_CONCAT DISTINCT multiple columns

From Dev

mysql GROUP_CONCAT DISTINCT on joined rows

From Dev

Can we make a DISTINCT of a group_concat(distinct somefield)?

From Dev

MySQL Use GROUP_CONCAT with Multiple JOINS

From Dev

MySQL - is it possible to use group_concat in an IN() statement?

From Dev

GROUP_CONCAT can't order by

From Dev

GROUP_CONCAT can't order by

From Dev

Mysql group_concat with distinct and where gives strange results

From Dev

group_concat mysql subquery only returns one item

From Dev

group_concat() on bit fields returns garbage in Mysql

From Dev

Invalid use of group function (group_concat and MySQL)

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

How can I use GROUP_CONCAT in Rails?

From Dev

How can I use group_concat on an entire subquery?

From Dev

GROUP_CONCAT DISTINCT BY ID

From Dev

how to use group_concat for some data like this?

From Dev

Can't get my head around GROUP_CONCAT

From Dev

MySQL Group_Concat Not In

From Dev

Group_concat use?

From Dev

Group_concat use?

From Dev

Mysql Group_concat where in again group concat how can i manage

From Dev

mysql group_concat returns null row if one entry in the group is null

From Dev

MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table

From Dev

MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

From Dev

MySQL's Group_Concat function miss the nulls. How can group the rows including NULLs.

Related Related

  1. 1

    MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

  2. 2

    MySQL GROUP_CONCAT(DISTINCT ...) ignores data

  3. 3

    SQL left join and group_concat returns duplicate data

  4. 4

    MySQL use GROUP_CONCAT when INSERTing data

  5. 5

    mysql GROUP_CONCAT DISTINCT multiple columns

  6. 6

    mysql GROUP_CONCAT DISTINCT on joined rows

  7. 7

    Can we make a DISTINCT of a group_concat(distinct somefield)?

  8. 8

    MySQL Use GROUP_CONCAT with Multiple JOINS

  9. 9

    MySQL - is it possible to use group_concat in an IN() statement?

  10. 10

    GROUP_CONCAT can't order by

  11. 11

    GROUP_CONCAT can't order by

  12. 12

    Mysql group_concat with distinct and where gives strange results

  13. 13

    group_concat mysql subquery only returns one item

  14. 14

    group_concat() on bit fields returns garbage in Mysql

  15. 15

    Invalid use of group function (group_concat and MySQL)

  16. 16

    how to use select query in a Group_concat sub query in mysql

  17. 17

    How can I use GROUP_CONCAT in Rails?

  18. 18

    How can I use group_concat on an entire subquery?

  19. 19

    GROUP_CONCAT DISTINCT BY ID

  20. 20

    how to use group_concat for some data like this?

  21. 21

    Can't get my head around GROUP_CONCAT

  22. 22

    MySQL Group_Concat Not In

  23. 23

    Group_concat use?

  24. 24

    Group_concat use?

  25. 25

    Mysql Group_concat where in again group concat how can i manage

  26. 26

    mysql group_concat returns null row if one entry in the group is null

  27. 27

    MySQL LIKE from JOIN clause and GROUP_CONCAT returns only one row from joined table

  28. 28

    MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

  29. 29

    MySQL's Group_Concat function miss the nulls. How can group the rows including NULLs.

HotTag

Archive