简体   繁体   English

MySQL Group_Concat按ID区分

[英]MySQL Group_Concat Distinct By Id

Is it possible to group_concat records by distinct Ids: 是否可以通过不同的id对group_concat记录:

GROUP_CONCAT(Column2 BY DISTINCT Column1)

I need to get the values from column2 by distinct values from column1 . 我需要通过column1的不同值来获取column2的值。 Because there are repeating values in column 2, that's why I can't use distinct on column2 . 因为第2列中有重复的值,所以我不能在column2上使用distinct。

Any thoughts on this? 有什么想法吗? Thanks! 谢谢!

EDIT 1 编辑1

Sample Table Records: 样本表记录:

ID  Value
1   A
1   A
2   B
3   B
4   C
4   C

Using the GROUP_CONCAT the I wanted [ GROUP_CONCAT(Value BY DISTINCT Id) ], I will have an output: 使用我想要的GROUP_CONCAT [ GROUP_CONCAT(Value BY DISTINCT Id) ],我将得到一个输出:

A, B, B, C

EDIT 2 编辑2

Somehow got my group_concat working: 不知何故让我的group_concat工作:

GROUP_CONCAT(DISTINCT CONCAT(Id, '|', Value))

This will display the concatenated values by distinct id, just need to get rid of the Id somewhere. 这将通过不同的ID显示连接的值,只需要删除某个地方的ID即可。 You can do it without the concat function, but I need the separator. 您可以在没有concat函数的情况下执行此操作,但是我需要使用分隔符。 This may not be a good answer but I'll post it anyway. 这可能不是一个好答案,但是我还是会发布它。

Try this one, ( the simpliest way ) 试试这个,( 最简单的方法

SELECT GROUP_CONCAT(VALUE)
FROM
(
    SELECT DISTINCT ID, VALUE
    FROM TableName
) a

SQLFiddle Demo SQLFiddle演示

GROUP_CONCAT函数支持DISTINCT表达式。

SELECT id, GROUP_CONCAT(DISTINCT value) FROM table_name GROUP BY id

这应该工作:

SELECT GROUP_CONCAT(value) FROM (SELECT id, value FROM table GROUP BY id, value) AS d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM