29

I have a query like this:

SELECT product.id,
       GROUP_CONCAT(image.id) AS images_id,
       GROUP_CONCAT(image.title) AS images_title,
       GROUP_CONCAT(facet.id) AS facets_id
...
GROUP BY product.id

And the query works, but not as expected, because if I have a product with 5 facets and 1 image (suppose an image with id=7), then I get something like this in "images_id":
"7,7,7,7,7"
If I have 2 images (7 and 3) then I get something like:
"7,7,7,7,7,3,3,3,3,3"
and in facets I get something like:
"8,7,6,5,4,8,7,6,5,4"

I think MySQL is making some type of union of the differents rows returned by the query, and then concatenating everything.

My expected result is (for the last example):

images_id = "7,3"
facets_id = "8,7,6,5,4"

I can obtain that using DISTINCT in the GROUP_CONCAT, but then I have another problem: If I have two images with the same title, one of them is ommited, and then I get something like:
images_id = "7,3,5"
images_title = "Title7and3,Title5"

So I miss the relation between images_id and images_title.

Does somebody know if it's possible to make this query in MySQL?

Maybe I'm complicating everything without any real benefits. I'm trying to execute only one query because performance, but now I'm not so sure if it's even faster to execute two queries (one for selecting the facets and another for the images for example).

Please explain what do you think is the best solution for this and why.

Thanks !

4 Answers 4

53

Just add DISTINCT.

Example:
GROUP_CONCAT(DISTINCT image.id) AS images_id

1
  • 1
    I think this needs to be expanded upon. ' DISTINCT ' => GROUP_CONCAT(DISTINCT image.id) AS images_id does not appear to be SQL syntax, and MySQL 5.1 throws a syntax error. The questioner didn't mention any programming languages, so if this answer isn't SQL it needs to be converted or explained.
    – Animism
    Dec 12, 2013 at 3:12
12

You'll need to get each group separately:

SELECT
    p.id,
    images_id,
    images_title,
    facets_id,
    ...
FROM PRODUCT p
JOIN (SELECT product.id, GROUP_CONCAT(image.id) AS images_id
      FROM PRODUCT GROUP BY product.id) a on a.id = p.id
JOIN (SELECT product.id, GROUP_CONCAT(image.title) AS images_title
      FROM PRODUCT GROUP BY product.id) b on b.id = p.id
JOIN (SELECT product.id, GROUP_CONCAT(facet.id) AS facets_id
      FROM PRODUCT GROUP BY product.id) b on c.id = p.id
...
5
  • I don't understand your query very well, but it looks very complex, I mean, we need a subselect with a group by for every concat? It looks like something very slow, is not faster 2 queries?
    – Enrique
    Sep 21, 2011 at 22:25
  • 3
    @Enrique, it's not about speed, it's about correctness. If my code doesn't need to actually work I can do everything instantly.
    – Johan
    Sep 21, 2011 at 22:34
  • @Enrique, You don't need a sub-select for every group concat, only for those tables where the match against p.id returns more than 1 row per p.id.
    – Johan
    Sep 21, 2011 at 22:35
  • Well, how I said in my question, it is about speed, I'm trying to select everything in just one query just because I want to be performant. Because is a lot easier to me to select all my data in different queries. But the GROUP_CONCAT is becoming so difficult that I'm not sure if I'm not killing MySQL instead of saving a few ms. That's why I was asking for a "best" solution (and the reason why you think that is the "best" solution). Anyway, I will test your query because is really interesting, I never made something like that.
    – Enrique
    Sep 21, 2011 at 23:21
  • Well I couldn't adapt your query for testing it, my query is much more complex that the posted in my question. I will go with the 2 query solution. Thanks !
    – Enrique
    Sep 22, 2011 at 0:05
5

You can add just the DISTINCT keyword, you'll get your desire results.

SELECT tb_mod.*, tb_van.*,
GROUP_CONCAT(DISTINCT tb_voil.vt_id) AS voil,
GROUP_CONCAT(DISTINCT tb_other.oa_id) AS other, 
GROUP_CONCAT(DISTINCT tb_ref.rp_id) AS referral
FROM cp_modules_record_tbl tb_mod 
LEFT JOIN cp_vane_police_tbl tb_van ON tb_van.mr_id= tb_mod.id
LEFT JOIN cp_mod_voilt_tbl tb_voil ON tb_voil.mr_id= tb_mod.id
LEFT JOIN cp_mod_otheraction_tbl tb_other ON tb_other.mr_id= tb_mod.id
LEFT JOIN cp_mod_referral_tbl tb_ref ON tb_ref.mr_id= tb_mod.id
WHERE tb_mod.mod_type = 2 GROUP BY tb_mod.id
0
1

If the issue is speed, then it may be a lot faster to simply select all the data you need as separate rows, and do the grouping in the application, i.e.:

SELECT product.id, image.id, image.title, facet.id

Then in the application:

foreach row:
    push product_id onto list_of_product_ids
    push image_id onto list_of_image_ids
etc.

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.