Postgresql: rows to array

Trabla: Postgresql: rows to array

Solving:

use function array_agg( <column> )

For example we have following table:

tbl_cars
id | name |  price
1  | bmw x5 | 50.000
2  | bmw x6 | 60.000
3  | toyota land cruiser | 80.000

And we want to select all IDs into ARRAY

Query:

SELECT 
               array_agg( cars.id )  AS bmw_ids
FROM 
               tbl_cars AS cars
WHERE
               cars.name LIKE '%bmw%'

RESULT:

bmw_ids bigint[]
-----------------------
{ 1, 2 }


No comments:

Post a Comment