【发布时间】:2017-04-27 18:46:13
【问题描述】:
我有一些关于电视节目的表格。此外,我还有一个页面显示用户可以在其中看到一些季节和剧集。
CREATE TABLE `tv` (
`tv_id` int(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(30),
`rating` float(2,1) DEFAULT NULL,
`total_seasons` tinyint UNSIGNED,
)
CREATE TABLE `tv_player_episode_mapping` (
`tv_id` int(11) UNSIGNED,
`season` tinyint(11) UNSIGNED,
`episode` tinyint(11) UNSIGNED,
`player_id` tinyint(11) UNSIGNED,
`file_name` TEXT
);
CREATE TABLE `player` (
`player_id` int(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
`hostname` VARCHAR(30),
UNIQUE KEY `name` (`hostname`)
);
要从表中选择数据,我使用以下查询:
SELECT tpe.season, tpe.episode, p.hostname, tpe.file_name, t.rating, t.total_seasons
FROM tv_player_episode_mapping tpe
INNER JOIN player p ON p.player_id = tpe.player_id
INNER JOIN tv t ON t.tv_id = tpe.tv_id
WHERE tpe.tv_id = 1;
以下是查询的结果,这里是问题所在 - 一些数据,如 total_seasons 和 ratins 重复:
[
{
file_name:"19891-molodoy-papa.html",
hostname:"kinoclub.cc",
season: 1,
episode: 1,
total_seasons: 1,
rating: 8.5,
},
{
file_name:"19891-molodoy-papa.html",
hostname:"kinoclub.cc",
season: 1,
episode: 2,
total_seasons: 1,
rating: 8.5,
}
]
我尝试更改我的查询以获得这样的想法(我是数据库新手,我认为这是服务器响应的良好结构):
{
total_seasons: 1,
rating: 8.5,
players: [
[ // Here first element of array `playes` is array for season 1
[ // Episode 1
{
file_name:"1-1-the-young-pope.html",
hostname:"my-player.io",
},
{
file_name:"1-1-the-young-pope.html",
hostname:"another-one-player.io",
}
],
[ // Episode 2
{
file_name:"1-2-the-young-pope.html",
hostname:"my-player.io",
},
{
file_name:"1-2-the-young-pope.html",
hostname:"another-one-player.io",
}
],
],
[] // Season 2
]
};
但由于我在数据库方面经验不足,因此我停止尝试使用此查询并且找不到方法:
SELECT tpe.season, tpe.episode,
JSON_OBJECT(
'total_seasons', t.total_seasons,
'players', JSON_OBJECT('hostname', p.name, 'file_name', tpe.file_name))
FROM tv_player_episode_mapping tpe
INNER JOIN player p ON p.player_id = tpe.player_id
INNER JOIN tv t ON t.tv_id = tpe.tv_id
WHERE tpe.tv_id = 1;
如何更改此查询以解决我的问题?
UPD:
我使用示例编写了一些查询,我很擅长,但现在我的查询结果看起来很奇怪。
新查询:
select json_object(
'rating', t.rating,
'total_seasons', t.total_seasons,
'players', json_array(
(select GROUP_CONCAT(
json_object(
'hostname', p.hostname,
'file_name', tpe.file_name,
'season', tpe.season,
'episode', tpe.episode
)
)
FROM tv_player_episode_mapping tpe
INNER JOIN player p ON p.player_id = tpe.player_id
INNER JOIN tv t ON t.tv_id = tpe.tv_id
where tpe.tv_id = 52)
)
)
from tv t WHERE tv_id=52;
查询结果:
{
"json_object( 'rating', t.rating, 'total_seasons', t.total_seasons, 'players', json_array( (select GROUP_CONCAT( json_object( 'hostname', p.hostname, 'file_name', tpe.file_name, 'season', tpe.season, 'episod":
"{\"players\": [\"{\\\"season\\\": 1, \\\"episode\\\": 1, \\\"hostname\\\": \\\"kinoclub.cc\\\", \\\"file_name\\\": \\\"19891-molodoy-papa.html\\\"},{\\\"season\\\": 1, \\\"episode\\\": 2, \\\"hostname\\\": \\\"kinoclub.cc\\\", \\\"file_name\\\": \\\"19891-molodoy-papa.html\\\"},{\\\"season\\\": 1, \\\"episode\\\": 1, \\\"hostname\\\": \\\"kinoclub.cc\\\", \\\"file_name\\\": \\\"123.html\\\"}\"], \"rating\": 8.5, \"total_seasons\": 1, \"rating\": 8.300000190734863}"
}
我可以做些什么来格式化这样的结果?
{
total_seasons: 1,
rating: 8.5,
players: []
};
【问题讨论】:
-
您无法从一个查询中获得一组行和一组字段。快速浏览一下,我最好的猜测是您需要两个查询 - 一个获取诸如评分之类的信息行,另一个获取有关玩家的行集,然后可以选择将一个嵌入到另一个中。
-
@LukeBriggs,不,那是谎言。在mysql中已经可以了。请看这个问题stackoverflow.com/questions/41094391/…
-
@Dennisrec 你误读了我的评论;
GROUP完全不同。 OP 在查询无法响应的 [field][field][group of rows] 之后。 -
这种将相关行分组到子数组中需要在应用语言中完成,不能直接在MySQL中完成。
-
@rel1x 看起来您缺少
CAST(..) as JSON部分 - 请参阅该答案中的下方信息:)
标签: mysql