简体   繁体   English

MySQL中的group_concat带有“ case when”条件

[英]group_concat in mysql with “case when ” conditions

I wrote sql code in mysql environment to concat the data . 我在mysql环境中编写了sql代码以连接数据。 But, I couldn't get the correct result , and I am confusing about what is wrong with my sql code. 但是,我无法获得正确的结果,并且我对自己的sql代码出了什么问题感到困惑。 my sql code is as follows: 我的SQL代码如下:

SELECT case when cc.complex_check_id = cmt.comp_o_id then cc.status cstatus,sgk.status sgstatus,cc.NAME complex_check_name,cc.min min_flag,cmt.comp_t_name cmpt_name,group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' '))
else cc.status cstatus,sgk.status sgstatus,cc.NAME complex_check_name,cc.min min_flag,'not' as cmpt_name,group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' ')) end 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' GROUP BY cc.NAME

could you give me some suggestions ,please ?...thanks a lot all of you ! 请给我一些建议吗?...非常感谢大家!

select group_concat(concat( case when ProStatus='A' then round(proQnty) else 0 end,':',prodtmappid) separator',') as result from tblproductmapforlisting where
 prodtmappid in (329607,329606,329605,329604) order by FIELD(prodtmappid,329607,329606,329605,329604)

The syntax you used for the CASE expression is not correct, you can only select one expression inside the case expression, but you selected more than one column, and I noticed that only one columns you need to select based on the case condition, so I moved all the columns out of the case expression except that column like this: 用于CASE表达式的语法不正确,您只能在case表达式中选择一个表达式,但是选择了不止一列,并且我注意到仅需要根据case条件选择一列,所以我将所有列移出case表达式,但该列除外:

SELECT 
  cc.status cstatus, 
  sgk.status sgstatus,
  cc.NAME complex_check_name, 
  cc.min min_flag,
  group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' ')),
  case when cc.complex_check_id = cmt.comp_o_id then cmt.comp_t_name 
                                                else 'not' as 
                                                end res_string 
FROM complex_check_anag cc ....
.... the rest of your query here

Also, you can rewrite your query using the INNER JOIN instead of the old join syntax like this: 此外,您还可以使用重写查询INNER JOIN ,而不是旧join这样的语法:

SELECT 
  cc.status cstatus, 
  sgk.status sgstatus,
  cc.NAME complex_check_name, 
  cc.min min_flag,
  group_concat(concat(concat(concat(concat(concat(f.NAME, ';') , sgk.NAME),' ') ,cc.operator),' ')),
  case when cc.complex_check_id = cmt.comp_o_id then cmt.comp_t_name 
                                                else 'not' as 
                                                end res_string 
FROM complex_check_anag cc, comp_t_anag cmt
INNER JOIN lnksinglechecktocomplexcheck lk ON cc.complex_check_id = lk.complex_check_id
INNER JOIN functionalci f ON f.id = lkcg.config_item_id  
INNER JOIN lnkconfigurationitemtosinglecheck lkcg ON sgk.single_check_id = lk.single_check_id
INNER JOIN single_check_anag sgk ON sgk.single_check_id = lkcg.single_check_id
WHERE sgk.status = 'active' 
GROUP BY cc.NAME

Note that, you didn't specified any condition between the two tables complex_check_anag cc, comp_t_anag cmt , so you will get a cartesian product between the two tables and it might not give you correct data. 请注意,您没有在两个表之间指定任何条件complex_check_anag cc, comp_t_anag cmt ,因此您将在两个表之间获得笛卡尔积,并且可能无法提供正确的数据。 So check the relation between these two tables and add a proper join type between them to get the correct data you are looking for. 因此,请检查这两个表之间的关系,并在它们之间添加适当的联接类型,以获取所需的正确数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM