合并ID重复的列的值

时间:2014-02-13 08:55:14

标签: sql ms-access concatenation ms-access-2010

我有一个带有ID列的表,可以包含重复项,因为它用作与我的主表匹配的外键。每个id可以有多个结果,如下例所示:

ID      FieldValue
1       This
2       Also this
1       and that
3       And additionally, this.
2       and, finally, this.

我如何合并FieldValue列,以便每个ID只有一行? E.g。

ID      FieldValue
1       This;and that
2       Also this;and, finally, this.
3       And additionally, this.

如果必须保留重复的行,我不会担心,例如

ID      FieldValue
1       This;and that
2       Also this;and, finally, this.
1       This;and that
3       And additionally, this.
2       Also this;and, finally, this.

因为我现在可以对它们运行DISTINCT查询,因为它们真的是重复的。谷歌搜索已经有一段时间没有结果;我怀疑是因为它有一些步骤,我需要知道这些步骤是什么,然后搜索才会富有成效。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您使用的是mysql,可以尝试这样的方法:

SELECT GROUP_CONCAT(FiledValue) As Result FROM tablename WHERE ID= '1'