mysql GROUP_CONCAT 重复项
我从 farmTOanimal 表中进行连接,如下所示。有一个类似的 farmTotool 表
id | FarmID | animal
1 | 1 | cat
2 | 1 | dog
当我在视图中加入表时,得到的结果如下所示
FarmID | animal | tool
1 | cat | shovel
1 | dog | shovel
1 | cat | bucket
1 | dog | bucket
现在,我执行 GROUP BY FarmID、GROUP_CONCAT(animal) 和 GROUP_CONCAT(tool),我得到了
FarmID | animals | tools
1 | cat,dog,cat,dog | shovel,shovel,bucket,bucket
但是,我真正想要的是什么是一个看起来像这样的结果。我该怎么做呢?
FarmID | animals | tools
1 | cat,dog | shovel,bucket
I make my join from a farmTOanimal table like this. There is a similar farmTotool table
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 | 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用
DISTINCT
选项:You need to use the
DISTINCT
option:另外,如果您想更改默认分隔符
,
使用以下语法:Also if you want to change the default separator
,
use this syntax :GROUP_CONCAT(不同的动物)
GROUP_CONCAT(DISTINCT animals)