mysql GROUP_CONCAT 重复

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4561650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:09:08  来源:igfitidea点击:

mysql GROUP_CONCAT duplicates

sqlmysqlgroup-concat

提问by Matt

I make my join from a farmTOanimal table like this. There is a similar farmTotool table

我从这样的 farmTOanimal 表中加入。有一个类似的farmTotool表

id | FarmID  | animal
 1 |    1    | cat
 2 |    1    | dog

When I join my tables in a view, I get a result that looks like this

当我在视图中加入我的表时,我得到的结果如下所示

FarmID | animal | tool
   1   |  cat   | shovel
   1   |  dog   | shovel
   1   |  cat   | bucket
   1   |  dog   | bucket

Now, I do GROUP BY FarmID, and GROUP_CONCAT(animal) and GROUP_CONCAT(tool), i get

现在,我按 FarmID 进行 GROUP_CONCAT(动物)和 GROUP_CONCAT(工具)分组,我得到

FarmID |     animals     |         tools
  1    | cat,dog,cat,dog | shovel,shovel,bucket,bucket

But, what I really want is a result that looks like this. How can I do it?

但是,我真正想要的是看起来像这样的结果。我该怎么做?

FarmID | animals |    tools
  1    | cat,dog | shovel,bucket

回答by grahamparks

You need to use the DISTINCToption:

您需要使用该DISTINCT选项:

GROUP_CONCAT(DISTINCT animal)