0

我有以下 mySql 选择语句,它返回以下结果并努力获得我想要的结果。

 select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
         if(count(distinct `episode`.`c12`),
         count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
        if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`
    from
        ((((`tvshow`
        left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
        left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
        left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
        left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
    group by `tvshow`.`idShow`
    having (count(`episode`.`c12`) > 0)

选择结果

选择结果

我正在尝试获得第 4 列,其中列出了季节,例如“第 1 季,第 2 季,第 3 季”

我可以通过运行以下选择来获取我需要的数据

select distinct c12 from episode where idShow = 1

它返回以下内容。

在此处输入图像描述

所以我想我可以使用替换将结果更改为“Season1”,但不知道如何让它只返回一个包含“Seasin1,Season2,Season3”的字符串,然后将其添加到顶部的选择语句中查看并将其整合在一起?

我试图得到的结果(使用 Photoshop 只是为了让你明白)

在此处输入图像描述

4

1 回答 1

1

只需添加GROUP_CONCAT(episode.c12)为附加列:

select `tvshow`.`idShow` AS `idShow`,`tvshow`.`c00` AS `ShowName`,
         if(count(distinct `episode`.`c12`),
         count(distinct `episode`.`c12`),0) AS `TotalSeasons`,
        if(count(`episode`.`c12`),count(`episode`.`c12`),0) AS `TotalEpisodeCount`,
        `GROUP_CONCAT(episode.c12)` as `Seasons`
    from
        ((((`tvshow`
        left join `tvshowlinkpath` ON ((`tvshowlinkpath`.`idShow` = `tvshow`.`idShow`)))
        left join `path` ON ((`path`.`idPath` = `tvshowlinkpath`.`idPath`)))
        left join `episode` ON ((`episode`.`idShow` = `tvshow`.`idShow`)))
        left join `files` ON ((`files`.`idFile` = `episode`.`idFile`)))
    group by `tvshow`.`idShow`
    having (count(`episode`.`c12`) > 0)

有关此 MySQL 特定函数的文档,请参阅http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

于 2013-09-24T16:19:24.657 回答