0

This is my databasedesign with a many-to-many relation

table Tournament
TournamentId int
name varchar(45)

table User
UserId int
name varchar(45)

table Tournament_Users
Tournament_Id int
User_Id int

I am attempting to return all tournaments with a list of the tournament's users in a JSON format. I'm not sure if this is possible to do in the query or it's a javascript job? I tried to follow this Returning child rows formatted as JSON in SQL Server queries but I drowned in the example.

This is my current query

SELECT t.TournamentId, t.Name as tourName, u.UserId, u.Name as userName
FROM  Tournament_Users tu
LEFT JOIN  Tournaments t ON tu.Tournament_Id = t.TournamentId
LEFT JOIN  Users u ON u.UserId = tu.User_Id

Query result

So as a json result this looks like this:

Json result 1

Which seems pretty difficult for a client to work with. How can I convert it into a format like this?

Json result 2

SQLFIDDLE: http://sqlfiddle.com/#!9/9bc2ebf/3

5
  • To format the output of the FOR JSON clause automatically based on the structure of the SELECT statement, specify the AUTO option. Commented May 2, 2018 at 11:23
  • Provide table structures (SHOW CREATE TABLE table) and example data for all table involved on sqlfiddle.com.. Then we can help you.. Commented May 2, 2018 at 11:34
  • Thanks, I edited the post @RaymondNijland Commented May 2, 2018 at 11:50
  • Dear, you do not mention here the database name. i was effort for u on MSSQL but u remarks me bad. Commented May 3, 2018 at 5:28
  • I didn't downvote you but it says mysql in both the title and the tag. Commented May 3, 2018 at 8:25

2 Answers 2

1

Try something like:

SELECT
  CONCAT('{"data": [', GROUP_CONCAT(`json`), ']}') `json`
FROM (
  SELECT
    JSON_OBJECT(
      'TournamentId', `t`.`TournamentId`,
      'tourName', `t`.`Name`,
      'users', JSON_ARRAYAGG(
                 JSON_OBJECT(
                   'UserId', `u`.`UserId`,
                   'userName', `u`.`Name`
                 )
               )
    ) `json`
  FROM
    `Tournament_Users` `tu`
    LEFT JOIN
      `Tournaments` `t` ON `tu`.`Tournament_Id` = `t`.`TournamentId`
    LEFT JOIN
      `Users` `u` ON `u`.`UserId` = `tu`.`User_Id`
  GROUP BY `t`.`TournamentId`, `t`.`Name`
  ORDER BY `t`.`TournamentId`
) `der`;

See db-fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks this works as intended. Keep in mind JSON_ARRAYAGG wasn't introduced until version 5.7.22
0

If you are using mysql 8.0 then

SELECT json_object(
'TournamentId', t.TournamentId ,'tourName', t.Name ,'UserId' ,u.UserId, 'UserName' ,u.Name)
FROM  Tournament_Users tu
LEFT JOIN  Tournaments t ON tu.Tournament_Id = t.TournamentId
LEFT JOIN  Users u ON u.UserId = tu.User_Id 

You can refer -https://dev.mysql.com/doc/refman/8.0/en/json-creation-functions.html

3 Comments

JSON_OBJECT isn't a MySQL 8.0 only function. JSON_OBJECT was already in MySQL in 5.7
This doesn't give me the format i desire as in the last image of my post instead it just gives me 4 rows in json format? Is there no way to squeeze it into 3 so I don't have duplicate rows for the same tournament? Thanks for your help
you have to use json_array with json_object.please check this- stackoverflow.com/questions/37470949/…

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.