group_concat重复值

时间:2015-05-04 14:55:40

标签: mysql select group-by group-concat

我写了两个查询来连接不同条件的值,两个查询的结果应该相同,但我无法通过query2得到正确的结果。两个查询如下:

QUERY1:

SELECT 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 ,lnkconfigurationitemtosinglecheck 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 sgk.single_check_id = lkcg.single_check_id and sgk.status = 'active' GROUP BY cc.NAME
使用query1

我可以得到以下结果:

res_string
---------------------------------------------------------------------------------------------------------- 
MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and 
MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and
Sito_Saturn_Secure;log in check and

QUERY2:

SELECT 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 ,lnkconfigurationitemtosinglecheck lkcg,comp_t_anag cmt
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 sgk.single_check_id = lkcg.single_check_id and sgk.status = 'active' and cc.complex_check_id <> cmt.comp_o_id GROUP BY sgk.NAME
使用query2

我可以得到以下结果:

res_string
-------------------------------------------------------------------------------------------------------------------------------
MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and ,MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and ,MIL04DNS01;memory check and ,MIL04DNS01;cpu_check and
MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and ,MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and ,MIL04APPBOXIP01;cpu_check and ,MIL04APPBOXIP01;memory check and 
Sito_Saturn_Secure;log in check and ,Sito_Saturn_Secure;log in check and ,Sito_Saturn_Secure;log in check and and ,MIL04DNS01;memory check and ,MIL04APPBOXIP01;memory check and  

你可以给我一些修改query2的结果以获得与query1相同的结果吗?...非常感谢。

1 个答案:

答案 0 :(得分:1)

以下是对您的一些建议:

  1. CONCAT功能可以根据需要接受任意数量的参数,因此您不需要concat(concat(concat...

  2. FROM tbl1,tbl2,tbl3...表时,绝不能使用JOIN。它应该由LEFT JOINRIGHT JOININNER JOINON规则明确设置。

  3. 在这里我猜你的查询应该是怎样的。但我有一些问题需要了解最后一个表comp_t_anag cmt必须如何加入。我没有看到加入该表的真正关键。我为所有表格做了LEFT JOIN,也许你需要INNER某个地方,所以你可以玩它。

  4.  SELECT GROUP_CONCAT(CONCAT(f.NAME, ';', sgk.NAME,' ',cc.operator,' ')) as res_string 
     FROM complex_check_anag cc
     LEFT JOIN lnksinglechecktocomplexcheck lk
     ON cc.complex_check_id = lk.complex_check_id 
     LEFT JOIN  single_check_anag sgk
     ON sgk.single_check_id = lk.single_check_id
       AND sgk.status = 'active'
     LEFT JOIN lnkconfigurationitemtosinglecheck lkcg
     ON sgk.single_check_id = lkcg.single_check_id 
     LEFT JOIN functionalci f 
     ON  f.id = lkcg.config_item_id 
     LEFT JOIN comp_t_anag cmt
     ON cc.complex_check_id <> cmt.comp_o_id
     GROUP BY sgk.NAME