Created
February 19, 2012 12:38
-
-
Save chanmix51/1863642 to your computer and use it in GitHub Desktop.
How to deal with objects in Postgresql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')"} | | |
+----+----------+----------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome :o