连接行上的mysql GROUP_CONCAT DISTINCT

时间:2013-10-27 13:14:10

标签: mysql sql myisam

我有一个看起来像这样的查询

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

出于某种原因或者说,我进入了结果字段" color"重复值,即" 1,2,1,2"。为什么DISTINCT不能用于连接的行?有没有机会解决或者我错过了一点?

1 个答案:

答案 0 :(得分:1)

DISTINCT ::

上尝试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