knex / knex

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.
https://knexjs.org/
MIT License
18.97k stars 2.1k forks source link

How to generated nested results ? #5974

Open prashantnirgun opened 1 year ago

prashantnirgun commented 1 year ago

Hi This is not any issue help is required, Let say I have user_groups and users table and users table has user_group_id column I want to generate output like this also has where condition users_group.branch_id = 10

[ { 'user_group.id' : 1, 'user_group.name' : 'Admin', 'user_group.branch_id' : 10, users : [ {'users.id' : 1, 'users.name' : 'Prashant'}, .... ] }]

can query builder does any kind or magic here other wise I have to manually parse the result set and generate the desired output.

mdNzaman commented 10 months ago

Not directly, but yes you can use group by and then collate for the desired result or use some high-order function like reduce to do that.