2013-09-24 87 views
0

我有下面的mySql select语句它返回下面的结果,并争取得到我后的结果。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) 

选择结果

Select Result

我想获得,将有在它如列出的赛季第4列 “第1季,第2季,第3季”

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

select distinct c12 from episode where idShow = 1

它返回以下内容。

enter image description here

,所以我想我可以使用替代来改变结果改为“第1季”,但不知道如何得到它只是返回一个包含“Seasin1,Season2,Season3”,然后将其添加一个字符串到视图顶部的select语句并将它们放在一起?

我试图让(使用Photoshop进行这只是让你能得到的想法)

enter image description here

+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) 

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat以了解此MySQL特定功能的文档。

+0

好的,谢谢,几乎工作我jsut不得不将它更改为'GROUP_CONCAT(distinct(episode.c12))',以便返回1,3,2例如因为'GROUP_CONCAT(episode.c12)'返回111111111,22222,333例如,现在只需要执行命令并替换部分 – justinf