in postgreSQL database I have table with Name(string) column.
ID| Name
1 | a
2 | b
3 | c
4 | d
With one query I would like to retrieve all names from the table into one string in the format:
'a', 'b', 'c', 'd'
in postgreSQL database I have table with Name(string) column.
ID| Name
1 | a
2 | b
3 | c
4 | d
With one query I would like to retrieve all names from the table into one string in the format:
'a', 'b', 'c', 'd'
You can use string_agg()
:
select string_agg(distinct '''' || name || '''', ', ' order by id)
from t;
For most purposes, though, I would suggest using arrays instead:
select array_agg(name order by id)
from t