0

Is there a way to get the output of a MySQL query to list rows in the following structure

{
    1:{voo:bar,doo:dar},
    2:{voo:mar,doo:har}
}

as opposed to

[
    {id:1,voo:bar,doo:dar},
    {id:2,voo:mar,doo:har}
]

which I then have to loop through to create the desired object?

I should add that within each row I am also concatenating results to form an object, and from what I've experimented with you can't group_concatenate inside a group_concatenation. As follows:

knex('table').select(
    'table.id',
    'table.name',
    knex.raw(
        `CONCAT("{", GROUP_CONCAT(DISTINCT
        '"',table.voo,'"',':','"',table.doo,'"'),
        "}") AS object`
    )
.groupBy('table.id')

Could GROUP BY be leveraged in any way to achieve this? Generally I'm inexperienced at SQL and don't know what's possible and what's not.

MikeyB
  • 460
  • 1
  • 6
  • 22

0 Answers0