Frage

I have a query who looks like this

SELECT
 producttemp.*,
 GROUP_CONCAT(colors.id) as color,
 GROUP_CONCAT(DISTINCT upc.UPC) as upc
FROM producttemp
INNER JOIN productcolor
 ON productcolor.productid=producttemp.id
INNER JOIN upc
 ON upc.productid=producttemp.id
INNER JOIN colors
 ON colors.id=productcolor.colorid
INNER JOIN categorie
 ON categorie.id = producttemp.productcategorie
GROUP BY producttemp.id
LIMIT 5

For some reason or antoher I get in the resultfield "color" duplicate values i.e. "1,2,1,2". Why does DISTINCT not work on joined rows? Is there any chance to work around or am I missing a point?

War es hilfreich?

Lösung

Try DISTINCT on colors.id::

SELECT
 producttemp.*,
 GROUP_CONCAT(DISTINCT colors.id) as color,
 GROUP_CONCAT(DISTINCT upc.UPC) as upc
FROM producttemp
INNER JOIN productcolor
 ON productcolor.productid=producttemp.id
INNER JOIN upc
 ON upc.productid=producttemp.id
INNER JOIN colors
 ON colors.id=productcolor.colorid
INNER JOIN categorie
 ON categorie.id = producttemp.productcategorie
GROUP BY producttemp.id
LIMIT 5
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top