PostgreSQL query to return results as a comma separated list

Viewed 131936

Let say you have a SELECT id from table query (the real case is a complex query) that does return you several results.

The problem is how to get all id return in a single row, comma separated?

6 Answers

use array_to_string() & array() function for the same.

select array_to_string(array(select column_name from table_name where id=5), ', ');

Use this below query it will work and gives the exact result.

SELECT array_to_string(array_agg(id), ',') FROM table

Output : {1,2,3,4,5}

SELECT array_agg(id, ',') FROM table

{1,2,3,4}

I am using Postgres 11 and EntityFramework is fetching it as array of integers.

Related