3 ms·
Two tables: Person: {id, name} and Hobbies: {person_id, name}. Then you can "SELECT person.name, array_agg(hobbies.name) FROM person JOIN hobbies ON (person.id
by wongarsu 9y ago
Two tables: Person: {id, name} and Hobbies: {person_id, name}.
Then you can "SELECT person.name, array_agg(hobbies.name) FROM person JOIN hobbies ON (person.id = hobbies.person_id)" to get your json representation back (at least with postgresql, "array_agg" isn't in standard SQL)
That's a pretty simple join, any real database layout I've seen goes much more complicated and much deeper than that.
- takeda 9y agoYour version is denormalized. It is faster, but also more likely to have misspellings and duplicates. It really depends what is more important.
- dotancohen 9y agoThe JSON example that it was compared to was also denormalized.
- mythrwy 9y agoExactly. You could put it in one table if you really wanted to (not that it would be ideal by any means).