2013-09-24 85 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