zachd1_618
zachd1_618

Reputation: 4340

SQLITE Select results into a string

I am looking for a method to return the results of an SQLite query as a single string for use in an internal trigger.

Something like Python's 'somestring'.join() method.

Table: foo
id    |    name
1     |     "foo"
2     |     "bar"
3     |     "bro"

Then a select statement:

MAGIC_STRING_CONCAT_FUNCTION(SELECT id FROM foo,",");

To return "1,2,3"

Upvotes: 1

Views: 1822

Answers (1)

Dan D.
Dan D.

Reputation: 74655

You're looking for the group_concat function:

group_concat((SELECT id FROM foo), ",");

The following is the description of the function group_concat from the documentation:

group_concat(X) group_concat(X,Y)

The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.

Upvotes: 2

Related Questions