Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Transform Columns to Comma Delimited String

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?

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.

like image 342
Vincy Avatar asked Sep 11 '12 15:09

Vincy


1 Answers

You can use GROUP_CONCAT():

select group_concat(yourColumn, ',')
from yourtable

See SQL Fiddle with Demo

like image 181
Taryn Avatar answered Nov 16 '22 17:11

Taryn