MySQL查询从不同的表和组数据中进行选择

时间:2016-12-12 20:08:01

标签: mysql

我有一些关于电视节目的表格。此外,我还有一个页面显示用户可以看到的季节和剧集。

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_seasonsratins等数据:

[
  {
    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: []
    };

1 个答案:

答案 0 :(得分:1)

简短的回答是否定的,但您仍然可以使用group_concat函数部分实现类似JSON的结构。

示例:

SELECT x, y, concat('[', group_concat(z), ']') as z FROM tbl group by x, y;

会给你答案:

x   y   z
1   2   [3,4,5]
6   7   [8,9,10]