group_concat 重复值

group_concat duplicate values

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

查询 1:

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

查询 2:

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. CONCAT 函数接受任意多的参数,所以你不需要 concat(concat(concat...

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

  3. 我猜你的查询应该是什么样的。但是我有一些问题要理解 last table comp_t_anag cmt 必须如何加入。我没有看到加入 table 的真正关键。我为所有 table 做了 LEFT JOIN,也许你在某个地方需要 INNER,所以你可以玩那个。

 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