SQLITE Select results into a string

孤街醉人 提交于 2021-01-28 07:05:32

问题


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"


回答1:


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.



来源:https://stackoverflow.com/questions/28257846/sqlite-select-results-into-a-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!