Last active
September 13, 2018 18:23
-
-
Save johnsnook/d7bb9fca61dfd990c31a18a1ef4298c4 to your computer and use it in GitHub Desktop.
Postresql child record ids as an array
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
| -- I don't understand how totally this works but it does so | |
| CREATE AGGREGATE children_ids (anyelement) | |
| ( | |
| sfunc = array_append, | |
| stype = anyarray, | |
| initcond = '{}' | |
| ); | |
| -- then you can do | |
| SELECT | |
| DISTINCT visitor.id, | |
| name, | |
| children_ids(visitor_id) as kids | |
| FROM | |
| visitor | |
| LEFT JOIN visits ON visitor.id = visits.visitor_id | |
| GROUP BY | |
| visitor.id | |
| -- and, in yii the $kids fields with be a string like so | |
| -- "{1421,4334,333,123214}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment