[英]Mysql Distinct select with replace
我有以下mySql select語句,它返回以下結果並努力獲取我想要的結果。
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”的字符串,然后將其添加到頂部的select語句中。查看並將其組合在一起?
我試圖獲得的結果 (為此使用了Photoshop,以便您可以理解)
只需將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 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.