【发布时间】:2023-04-06 13:30:02
【问题描述】:
我正在开发一个与 MySQL 数据库连接的应用程序。
我在显示数据时遇到问题。
这是我在数据库中的选择
SELECT
a_activity.id,
team.name,
b_activity.team_id,
a_activity.number
FROM a_activity,
team,
b_activity
WHERE team.id = b_activity.team_id
AND b_activity.id = a_activity.team_activity_id;
这是我在 API 链接上的结果
{
"statistic": [
{
"id": 1,
"name": "Test A",
"team_id": 1,
"number": "103,122"
},
{
"id": 2,
"name": "Test 18",
"team_id": 2,
"number": "113,132"
},
{
"id": 3,
"name": "Test 15",
"team_id": 3,
"number": "133,129"
},
{
"id": 4,
"name": "Test 10",
"team_id": 4,
"number": "113,112"
},
{
"id": 1,
"team_name": "Test A",
"team_id": 1,
"number": "123,152"
}
]
}
但我想将结果(数字)放在 JSON 的同一行或数据库的同一列中。我希望我的 JSON 类似于帖子下的这个示例。
{
"statistic": [
{
"id": 1,
"name": "Test A",
"team_id": 1,
"number": "103,122, 123,152"
},
{
"id": 2,
"name": "Test 18",
"team_id": 2,
"number": "113,132"
},
{
"id": 3,
"name": "Test 15",
"team_id": 3,
"number": "133,129"
},
{
"id": 4,
"name": "Test 10",
"team_id": 4,
"number": "113,112"
}
]
}
感谢您的帮助!
【问题讨论】:
-
今日提示:切换到现代、明确的
JOIN语法。更容易编写(没有错误),更容易阅读(和维护),并且在需要时更容易转换为外连接。 -
了解重新排列数组。我发现掌握它是一项非常困难的任务(我仍然很挣扎),但有回报