1

I am trying to receive all of the players under a certain team along with the team's id.

Here is my query:

SELECT player.team_id, team.name, player.first_name, player.last_name FROM player, team WHERE player.team_id = team.id AND player.league = "League 1";

I am taking the result (stored in teams) and sending it to my client by doing this:

res.send({
        success: true,
        teams: teams
      });

Here is what it returns when I reach its endpoint:

enter image description here

I want to reformat this date to where I get one field for each unique team. It should have a players field (array) inside so I can access the names of each player part of the team. Does anyone know how I can manipulate this data to that format?

So for example, for this data set, inside the teams object, there would only be one field named "Warriors" with a players array that has all of those people's name inside of it.

I tried following the answer, but now it's just returning it as a string, not an actual object.

enter image description here

1 Answer 1

1

You can use aggrgation and json functions:

select
    t.team_id,
    t.name,
    json_array_agg(
        json_object('first_name', p.first_name, 'last_name', p.last_name)
    ) as players
from teams t
inner join players p on p.team_id = t.team_id
group by t.team_id, t.name

The third column in the resultset is a json array that contains json objects that represent the players, with keys first_name and last_name.

You can also aggregate the whole resulset as a json array, with another level of aggreation:

select json_array_agg(
    json_object('team_id', team_id, 'name', name, 'players', players) 
) res
from (
    select
        t.team_id,
        t.name,
        json_array_agg(
            json_object('first_name', p.first_name, 'last_name', p.last_name)
        ) as players
    from teams t
    inner join players p on p.team_id = t.team_id
    group by t.team_id, t.name
) t

Edit: json_array_agg() is available starting version 10.5.0 only. In earlier versions, an alternative is string concatenation:

select
    t.team_id,
    t.name,
    concat(
        '[',
        group_concat(json_object('first_name', p.first_name, 'last_name', p.last_name)),
        ']'
    ) as players
from teams t
inner join players p on p.team_id = t.team_id
group by t.team_id, t.name
Sign up to request clarification or add additional context in comments.

8 Comments

Getting an error. My MySQL is based on MariaDB I believe. Getting this: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '.name, JSON_ARRAYAGG( JSON_OBJECT('first_name', p.first_name, 'las' at line 3
@legit98: there is a missing comma... fixed.
Getting this now - FUNCTION db.JSON_ARRAYAGG does not exist. I tried using the function you provided as well.. I may need to update my MySQL version. Is there any other way of accomplishing the restructuring? Rather not update.
@legit98: which version are you running? select version() will tell you.
10.4.11-MariaDB
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.