【问题标题】:group_concat duplicate valuesgroup_concat 重复值
【发布时间】:2015-07-14 00:35:31
【问题描述】:

我写了两个查询来连接不同条件的值,两个查询的结果应该是一样的,但是我用query2不能得到正确的结果。两个查询如下:

查询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 相同的结果吗?...非常感谢。

【问题讨论】:

  • 仅供参考,我什至不考虑查看查询,只要它们的格式是这样的。实际上它们根本没有格式化。当您使用它时,您可以熟悉 1992 年成为标准的连接语法并立即使用它。这只是一个难以理解的混乱。
  • 能否提供 sqlfiddle 数据样本??

标签: mysql select group-by group-concat


【解决方案1】:

以下是给你的一些建议:

  1. CONCAT函数接受任意数量的参数,所以你不需要concat(concat(concat...

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

  3. 我猜你的查询应该是什么样子。但是我有一些问题要理解最后一个表comp_t_anag cmt 必须如何加入。我没有看到加入该表的真正关键。我为所有桌子都做了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

【讨论】:

    猜你喜欢
    • 2012-07-14
    • 2013-11-21
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2011-12-11
    • 2018-04-21
    • 2014-12-22
    • 2016-05-26
    相关资源
    最近更新 更多