SQLite将列转换为逗号分隔的字符串

时间:2022-10-22 00:19:12

I have a table with rows of data and need to create a comma delimited string for all the rows for a column. Can this be achieved just using the the SELECT statement in SQLite or I have to get the data into Cursor and build the string by iterating through it?

我有一个包含数据行的表,需要为列的所有行创建逗号分隔的字符串。这可以只使用SQLite中的SELECT语句来实现,或者我必须将数据导入Cursor并通过迭代来构建字符串吗?

For Example:

例如:

UserId
1df4181d-6c52-4aa3-926f-2dacb0a68c70
1df4181d-6c52-4aa3-926f-2dacb0a68c71
1df4181d-6c52-4aa3-926f-2dacb0a68c72
1df4181d-6c52-4aa3-926f-2dacb0a68c73
1df4181d-6c52-4aa3-926f-2dacb0a68c74
1df4181d-6c52-4aa3-926f-2dacb0a68c75

into

1df4181d-6c52-4aa3-926f-2dacb0a68c70,1df4181d-6c52-4aa3-926f-2dacb0a68c71,1df4181d-6c52-4aa3-926f-2dacb0a68c72,1df4181d-6c52-4aa3-926f-2dacb0a68c73,1df4181d-6c52-4aa3-926f-2dacb0a68c74,1df4181d-6c52-4aa3-926f-2dacb0a68c75

Any suggestions would be greatly appreciated.

任何建议将不胜感激。

1 个解决方案

#1


23  

You can use GROUP_CONCAT():

您可以使用GROUP_CONCAT():

select group_concat(yourColumn, ',')
from yourtable

See SQL Fiddle with Demo

请参阅SQL Fiddle with Demo

#1


23  

You can use GROUP_CONCAT():

您可以使用GROUP_CONCAT():

select group_concat(yourColumn, ',')
from yourtable

See SQL Fiddle with Demo

请参阅SQL Fiddle with Demo