Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created February 19, 2012 12:38
Show Gist options
  • Save chanmix51/1863642 to your computer and use it in GitHub Desktop.
Save chanmix51/1863642 to your computer and use it in GitHub Desktop.
How to deal with objects in Postgresql
SELECT author FROM author;
| author |
+-------------------+
| "(1,'john doe')" |
+-------------------+
| "(2,'Edgar')" |
+-------------------+
SELECT
author.id,
author.name,
array_agg(post) AS posts
FROM
author
LEFT JOIN post ON
author.id = post.author_id
GROUP BY
author.id;
| id | name | posts |
+----+----------+----------------------------------------+
| 1 | John Doe | {"(1,'first post')","(2,'new post')"} |
+----+----------+----------------------------------------+
| 2 | Edgar | {"(3,'Hello world')"} |
+----+----------+----------------------------------------+
@Palleas
Copy link

Palleas commented Feb 19, 2012

Awesome :o

@stof
Copy link

stof commented Feb 19, 2012

😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment