我们如何使用带有group_concat和case条件的MySQL select语句?

时间:2015-04-02 09:24:29

标签: mysql case group-concat

我用group_concat写了mysql select语句,我可以得到正确的结果。但是,如果我用case dondition修改我的select语句,我无法获得校正结果。我的选择声明如下:

SELECT replace(concat(concat(complex_check_name, '='), res_string), ',', '') results FROM 
(SELECT cc.NAME complex_check_name, group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' ')) as res_string
FROM complex_check_anag cc,lnksinglechecktocomplexcheck lk,single_check_anag sgk,functionalci f ,lnkciitemtocomplexcheck lkcg 
WHERE cc.complex_check_id = lk.complex_check_id AND sgk.single_check_id = lk.single_check_id and f.id = lkcg.config_item_id 
and cc.complex_check_id = lkcg.compx_check_id GROUP BY cc.NAME) x

使用这个select语句,我可以得到以下结果:

checks CO04=INFRAVICO04;cpu check CO04 and INFRAVICO04;disk check CO04 and INFRAVICO04;mem check CO04 and 
checks CO05=INFRAVICO05;cpu check CO05 or INFRAVICO05;disk check CO05 or INFRAVICO05;mem check CO05 or 
checks DB01=INFRASEPDB01;mem check DB01 and INFRASEPDB01;cpu check DB01 and INFRASEPDB01;disk check DB01 and 

我想将“&”替换为“&”,“或”替换为“||”在结果中;该值位于“cc.operator”字段中。所以,我修改了之前的select语句如下:

SELECT replace(concat(concat(complex_check_name, '='), res_string), ',', '') results FROM 
(SELECT cc.NAME complex_check_name, group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,
CASE cc.operator when 'and'  then '&' when 'or' then '||' END CASE),' ')) as res_string 
FROM complex_check_anag cc,lnksinglechecktocomplexcheck lk,single_check_anag sgk,functionalci f ,lnkciitemtocomplexcheck lkcg 
WHERE cc.complex_check_id = lk.complex_check_id AND sgk.single_check_id = lk.single_check_id and f.id = lkcg.config_item_id 
and cc.complex_check_id = lkcg.compx_check_id GROUP BY cc.NAME) x

但是,有一条错误消息,我无法得到正确的结果。你能给我一些建议吗? 我真的很感谢你的帮助!

0 个答案:

没有答案